Monday, 27 July 2015

Difference between Single & First in LINQ

Single:
DBContext db = new DBContext();
Customer customer = db.Customers.Where( c=> c.ID == 5 ).Single();
The single  throws exception if multiple records are returned.

The First method provides the first record returned by the query.

First
BContext db = new DBContext();
NewsItem newsitem = db.NewsItems.OrderByDescending( n => n.AddedDate ).First();

No comments:

Post a Comment