When to use .First and when to use .FirstOrDefault with LINQ?
I've searched around and haven't really found a clear answer as to when you'd want to use .First
and when you'd want to use .FirstOrDefault
with LINQ.
When would you want to use
.First
? Only when you'd want to catch the exception if no results where returned?var result = List.Where(x => x == "foo").First();
And when would you want to use
.FirstOrDefault
? When you'd always want the default type if no result?var result = List.Where(x => x == "foo").FirstOrDefault();
And for that matter, what about Take?
var result = List.Where(x => x == "foo").Take(1);