In regards to the above and/or including other methods, if you are searching for one record, and only one record exists, which one would perform the fastest? For example, I want to make sure that once it finds the value being queried, I'm looking for one that will return it right away without searching through the remaining records.
-
2It depends on the query provider. – SLaks Jun 14 '12 at 23:13
-
1`Any` does not return a value, it returns a flag that indicates whether a value exists or not. – Sergey Kalinichenko Jun 14 '12 at 23:13
-
2Any() doesn't return a record. FirstOrDefault is *probably* the fastest way to get a single record, but doesn't guarantee there's only one. – Blorgbeard Jun 14 '12 at 23:14
-
Good catch.. But just for peace of mind, I assume it returns as soon as it finds a matching value? – Shane LeBlanc Jun 14 '12 at 23:14
-
That would be the sensible implementation. You can probably assume it to be the case. – Blorgbeard Jun 14 '12 at 23:15
-
1The semantics of these methods are not the same. – Brian Rasmussen Jun 14 '12 at 23:21
-
Good Reads at [LINQ: When to use SingleOrDefault vs. FirstOrDefault with filtering criteria](http://stackoverflow.com/q/1745691/9664) and [When to use .First and when to use .FirstOrDefault with LINQ?](http://stackoverflow.com/q/1024559/9664) – Metro Smurf Jun 14 '12 at 23:32
-
To reiterate: **it depends on the query provider**. There's nothing stopping the implementer of a query provider choosing to scan the whole table when asked for `First()`! Sure, it would be a perverse and unpopular thing to do, but all they promise is the *semantics* of the call, not its implementation. For *specific* query providers this is a perfectly fine question to ask. See for example Jon Skeet's [EduLinq](http://msmvps.com/blogs/jon_skeet/archive/tags/Edulinq/default.aspx) series for a closer look at what LINQ to objects does, for an extended example. – AakashM Jun 15 '12 at 08:02
2 Answers
If you have a think about this you can probably work it out.
FirstOrDefault
enumerates the set until it finds a matching item
SingleOrDefault
enumerates the entire collection to ensure the item occurs exactly once
This means SingleOrDefault
cant be faster than FirstOrDefault
. But it does depend a little on the query providers implementation
EDIT:
Any can be implemented even faster. Concider a SQL implementation:
Select Top 1 from myTable //(its not quite this but this implementation but it will be similar)
will execute faster than:
Select Top 1 from myTable where <somecondition>

- 33,537
- 22
- 129
- 198
-
One last thing, if no record exists, is a null returned? I "think" it was Single() I had used and when nothing came back an exception was thrown. – Shane LeBlanc Jun 14 '12 at 23:23
-
If no records exist both singleOrDefault and FirstOrDefault return the default value of the object (in most cases this is null). With .First or .Single an exception is thrown if the collection does not contain a match – undefined Jun 14 '12 at 23:31
-
1See also... [LINQ: When to use SingleOrDefault vs. FirstOrDefault with filtering criteria](http://stackoverflow.com/q/1745691/9664) and [When to use .First and when to use .FirstOrDefault with LINQ?](http://stackoverflow.com/q/1024559/9664) – Metro Smurf Jun 14 '12 at 23:32
Single (and SingleOrDefault) should be only used when you want to force an exception if there are 0 or more than one results. The typical SQL implementation would be
Select Top 2 * from table
First on the other hand will typically short-circuit after the first match is found. In TSQL
Select Top 1 * from table
Any is used to indicate if at least one match is found (and short circuits after finding it). In TSQL, this uses the Exists.
In your case, since you need the result value, there is no use in making a separate request (with Any) and then perform Single. Instead just use FirstOrDefault and then check for null on the return result.
var foo = table.FirstOrDefault(t => t.bar == val);
if (null != foo)
...
Select 1 from table where Exists

- 10,169
- 1
- 25
- 43