Pawan Nogariya
Pawan Nogariya

Reputation: 8960

Does number of select columns affects efficiency in linq query?

According to this post

Does the number of columns returned affect the speed of a query?

we should avoid Select * for sql

Does this applies to linq queries also?

Edit:

From Select * for linq I mean

from a in mytable select a;

and from selected columns I mean

from a in mytable select new{a.Prop1, a.Prop2};

Upvotes: 0

Views: 318

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236218

You cannot do SELECT * with LINQ. Generated query always have all column names to be selected. So, there will not be delay caused by reading table's definition in order to determine columns of that table. But there will be delay with reading all columns data from disk and sending data across the network. So, if you need only several columns from table, then it's better to select only those columns:

var query = from p in db.Person
            select new { p.Id, p.Forename, p.Surname };

REMARK: With Linq to Object (in-memory query) you have opposite situation - you already have object in memory. Selecting set of properties is slower, because anonymous object should be created, and it's properties should be mapped. But this operation does not uses hard drive or network, so delay is insignificant.

Upvotes: 3

Aniket Inge
Aniket Inge

Reputation: 25705

LINQ queries, if done on a dataset or collection happen in memory. The impact isnt as high as it is in DB level because DB stores data in a hard-drive.

Since, your RAM is faster than your HDD, the performance isn't noticeable. But its still not a bad idea to apply it to LINQ

Upvotes: 0

Related Questions