Showing posts with label First() VSSingle() LINQ. Show all posts
Showing posts with label First() VSSingle() LINQ. Show all posts

Monday, 14 July 2014

Difference between First() and Single() methods in LINQ.


Difference between First() and Single() methods in LINQ.


First() - There is at least one result, an exception is thrown if no result is returned. 

DBContext db = new DBContext();
Customer customer = db.Customers.Where( c=> c.ID == 5 ).First();

 


Single() - There is exactly 1 result, no more, no less, an exception is thrown if no result is returned. 

DBContext db = new DBContext();
Customer customer = db.Customers.Where( c=> c.ID == 5 ).Single();