When to use Single, SingleOrDefault, First and FirstOrDefault?

You should take care of following points while choosing Single, SingleOrDefault, First and FirstOrDefault.

  1. When you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault.
  2. When you want a default value is returned if the result set contains no record, use SingleOrDefault.
  3. When you always want one record no matter what the result set contains, use First or FirstOrDefault.
  4. When you want a default value if the result set contains no record, use FirstOrDefault.
Tagged , . Bookmark the permalink.

Leave a Reply