Consider the IEnumerable extension methods SingleOrDefault()
and FirstOrDefault()
MSDN documents that SingleOrDefault:
I don't understand why you're using FirstOrDefault(x=> x.ID == key)
when this could retrieve results much faster if you use Find(key)
. If you are querying with the Primary key of the table, the rule of thumb is to always use Find(key)
. FirstOrDefault
should be used for predicate stuff like (x=> x.Username == username)
etc.
this did not deserve a downvote as the heading of the question was not specific to linq on DB or Linq to List/IEnumerable etc.