jason
jason

Reputation: 7164

Does LINQ Select affect on performance?

Let's say I have an object of a list of objects which has 30 properties like this :

List<myObject> objectList = db.myObject.Where(x => x.Brand == "Ford").ToList();

And objectList has about 250k members.

If I get only two properties from db and make a list like this :

List<myObject2> objectList2 = (from x in db.myObject
                                 where x.Brand == "Ford"
                                  select new myObject2 {Brand = x.Brand, userId = x.userId}).ToList();

This list also has 250k members but the objects in this list has 2 properties. Would traversing in the second list be faster than the first one? Or would it be same? Please enlighten me. Thanks.

Upvotes: 1

Views: 1271

Answers (1)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

Your Linq-statements are translated to SQL - something like this

select brand, userId from myTable where Brand == "Ford"

When you omit the projection within your Linq-statement the following sql is emited instead:

select * from myTable where Brand == "Ford"

This returns all columns from your table. So of course specifiying the returned columns should be faster in this case than just omitting the projection.

EDIT: However the performance of traversing from one element within your ResultSet to the next won´t shouldn´t have a big impact, the bigger one is the actual size of data to be fetched from the database.

Upvotes: 2

Related Questions