Thursday, September 17, 2015

Put a Lambda expression inside of a Single() or a FirstOrDefault in C#.

Category category = categories.Where(x => x.CategoryId == whatever).Single();

 
 

...may be rewritten as...

Category category = categories.Single(x => x.CategoryId == whatever);

 
 

This is also legit code:

Category category = categories.FirstOrDefault(x => x.CategoryId == whatever);

No comments:

Post a Comment