Monday, July 15, 2013

Reading - Programming Entity Framework: DbContext - Quote from Julie Lerman

The LINQ query looks the same as any other query that filters based on name. We then
use the Singlemethod to let Entity Framework know that we expect a single result. If
the query returns no results, or more than one result, an exception will be thrown. If
there are potentially no matches, you can use the SingleOrDefault method, which will
return  null if  no  results  are  found.  Example  2-16 shows  the  FindGreatBarrierReef
method updated to account for the fact it may not exist in the database.

If two rows are found,  Singleand  SingleOrDefault will throw an error because there is not a
single result. If you just want the first result, and aren’t concerned if there is more than
one result, you can use Firstor FirstOrDefault.
Post a Comment