Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
LINQ

Differences between FirstOrDefault and SingleOrDefault in LINQ

- 14.12.22 - ErcanOPAK

Whenever you use SingleOrDefault, you clearly state that the query should result in at most a single result. On the other hand, when FirstOrDefault is used, the query can return any amount of results but you state that you only want the first one.

If your result set returns 0 records:

  • Single throws an exception
  • First throws an exception
  • SingleOrDefault returns the default value for the type (e.g. default for int is 0)
  • FirstOrDefault returns the default value for the type

If your result set returns 1 record:

  • Single returns that record
  • First returns that record
  • SingleOrDefault returns that record
  • FirstOrDefault returns that record

If your result set returns many records:

  • Single throws an exception
  • First returns the first record
  • SingleOrDefault throws an exception
  • FirstOrDefault returns the first record

There is

  • a semantical difference
  • a performance difference

between the two.

Semantical Difference:

  • FirstOrDefault returns the first item of potentially multiple (or default if none exists).
  • SingleOrDefault assumes that there is a single item and returns it (or default if none exists). Multiple items are a violation of the contract, and an exception is thrown.

Performance Difference

  • FirstOrDefault is usually faster, it iterates until it finds the element and only has to iterate the whole enumerable when it doesn’t find it. In many cases, there is a high probability to find an item.
  • SingleOrDefault needs to check if there is only one element and therefore always iterates the whole enumerable. To be precise, it iterates until it finds a second element and throws an exception. But in most cases, there is no second element.

Conclusion

  • Use FirstOrDefault if you don’t care how many items there are or when you can’t afford to check uniqueness (e.g. in a very large collection). When you check the uniqueness of adding the items to the collection, it might be too expensive to check it again when searching for those items.
  • Use SingleOrDefault if you don’t have to care about performance too much and want to make sure that the assumption of a single item is clear to the reader and checked at runtime.

In practice, you use First / FirstOrDefault often even in cases when you assume a single item, to improve performance. You should still remember that Single / SingleOrDefault can improve readability (because it states the assumption of a single item) and stability (because it checks it) and use it appropriately.

 

Briefly, we can say that

If you want an exception to be thrown if the result set contains many records, use SingleOrDefault.

If you always want 1 record no matter what the result set contains, use FirstOrDefault

Related posts:

What is the difference between Select and SelectMany in Linq
Post Views: 6

Post navigation

How to reset identity seed after deleting records in SQL
What is the difference between Select and SelectMany in Linq

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

January 2023
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
« Dec    

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (512)
  • Add Constraint to SQL Table to ensure email contains @ (288)
  • Get the User Name and Domain Name from an Email Address in SQL (274)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (263)
  • Find numbers with more than two decimal places in SQL (246)
  • ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn (230)
  • Confirm before process with ASPxButton in Devexpress (229)
  • How to solve “Response.Redirect cannot be called in a Page callback” for DevExpress Components (221)
  • How to make some specific word(s) Bold or Underline in ReportViewer (205)
  • Devexpress ASPxGridview Column Grouping in Code (205)

Recent Posts

  • What is the difference between ‘ref’ and ‘out’ keywords in C#
  • How to check if javascript is enabled on the client’s browser
  • How to disable ASP.Net button after click to prevent double clicking
  • What is the difference between HashSet and List in .net?
  • What is the purpose of nameof in C#?
  • What is the difference between Select and SelectMany in Linq
  • Differences between FirstOrDefault and SingleOrDefault in LINQ
  • How to reset identity seed after deleting records in SQL
  • The Ternary Operator in C# (?:)
  • The Null Conditional Operator in C# (?.)

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (512)
  • Add Constraint to SQL Table to ensure email contains @ (288)
  • Get the User Name and Domain Name from an Email Address in SQL (274)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (263)
  • Find numbers with more than two decimal places in SQL (246)

Recent Posts

  • What is the difference between ‘ref’ and ‘out’ keywords in C#
  • How to check if javascript is enabled on the client’s browser
  • How to disable ASP.Net button after click to prevent double clicking
  • What is the difference between HashSet and List in .net?
  • What is the purpose of nameof in C#?

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2023 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com