Difference between FirstOrDefault() and SingleOrDefault() method in LINQ.
FirstOrDefault() : Gets the first item that matches a given criteria.
var users = from user in dataContext.Users where user.Id==5 select user;
var firstUser = users.FirstOrDefault();
|
SingleOrDefault() : If you specify this extension method
that means you are specifically saying that there can be only one value that
matches the criteria. If there are more than 1 value that matches the criteria,
throw an exception.
var users = from user in dataContext.Users where user.Id==5 select user;
var singleUser = users.SingleOrDefault();
|
No comments:
Post a Comment