tags:

views:

81

answers:

4

In general, is one faster than the other, assuming 1 record is being returned?

Are there benefits to using one over the other?

Just as an example:

DataContext.TableBlah.FirstOrDefault(_blah => _blah.id == 1);

or

var test = (from blah in TableBlah
            where blah.id == 1
            select blah)
+2  A: 
var test = (from blah in TableBlah
            where blah.id == 1
            select blah)

This can return more than 1 rows, for matching records (As against FirstOrDefault).

Performance wise, I don't think it should be any different.
It also depends on number of rows in the table? Is Id column indexed?

shahkalpesh
Yes the Id column is indexed...
Mike Fielden
@Mike: Then the difference should be same as running SELECT * FROM table WHERE ID = 1 vs SELECT TOP 1 * FROM TABLE WHERE ID = 1. If there is only 1 record matching the ID, performance should not be a concern because the column is indexed.
shahkalpesh
+1  A: 

FirstOrDefault returns as soon as it finds a result, so it can be slightly faster, but not by an order of magnitude... Anyway, your second query can return more than one result, so it's not really a fair comparison. You could compare it to SingleOrDefault instead, which is like a Select, but returns just 1 result.

SingleOrDefault, like a Select, must go through the complete list, but it guarantees that you get one and only one result.

Note: If you're using Linq to SQL, than you second query can be faster, depends on your indexes...

Meta-Knight
A: 

This can potentially be faster.

var test = (from blah in TableBlah
        where blah.id == 1
        select blah)

It depends on which LINQ provider you're using. If you're using LINQ to Sql or Subsonic, etc, this can get translated into a direct SQL call, which will only fetch one row from the DB.

In LINQ to Objects, the two statements will be nearly identical, due to LINQs streaming of results.

Reed Copsey
@Reed: I think you are assuming that the table has only 1 row with id = 1 when you said "which will only fetch one row from the DB". Is that right?
shahkalpesh
Yes. That's with that assumption in place. LINQ to Sql and MS SQL server will be nearly identical, too, since it'll optimize the other option with SELECT TOP, but I believe subsonic an some other linq providers will not, so they'll select all rows, and stream until you find one. As I said, it's a ~potential~ improvement, but a lot depends on your environment.
Reed Copsey
+1  A: 

I'm 90% certain that

var test = dc.TableBlah.FirstOrDefault(_blah => _blah.id == 1);

sets up the exact same expression tree as

var test = (from blah in dc.TableBlah
            where blah.id == 1
            select blah).FirstOrDefault();

So your second example simply lacks the benefit of getting a single record by calling FirstOrDefault(). In terms of performance, they'll be identical.

Personally, I would use SingleOrDefault() instead, since you're looking for a single item. SingleOrDefault() will throw if you receive more than one record.

Randolpho