0

I have the following code:

var latest = _containerClient.GetBlobs(prefix: path).OrderByDescending(m => m.Properties.LastModified).ToList().First();
var name = latest.Name;

If the path doesn't exist, I see the error: Sequence contains no elements

How do I handle this error so that if there are blobs, return error string?

user989988
  • 3,006
  • 7
  • 44
  • 91
  • 3
    At first sight I would say: Change _First_ with _FirstOrDefault_ Then check if _latest_ is null before using it. See the docs or this post: https://stackoverflow.com/questions/1024559/when-to-use-first-and-when-to-use-firstordefault-with-linq – Steve Jul 19 '22 at 06:26
  • 2
    There is no reason to use `ToList().First()` - just do `.First()` or `.FirstOrDefault()` - though methinks you _should_ be using the `Async` methods instead. – Dai Jul 19 '22 at 06:28

0 Answers0