X

Linq Sequence contains no matching element

linq

So sometimes you will get these strange errors or exceptions when using Linq Sequence contains no matching element

For example if you use

var result = myEnumerableCollection.First(x => x.Name == person.Name);

This will throw InvalidOperationException with Sequence contains no matching element.

But if you use

var result = myEnumerableCollection.FirstOrDefault(x => x.Name == person.Name);
if(result == null)
 //did not find in the collection something went wrong or .....

Which will return you a null if its not found then you can check if the result is null and continue on with your work 🙂

Hope this helps 🙂

Categories: .NET Misc
Taswar Bhatti:

View Comments (6)

Related Post