Performance in linq to entity queries between linq and lambda?

I have a doubt in a query that it's a little bit slow and i want to know what's best for performance.

Let's present this first example:

var result = from tableA in context.TableA
             join tableB in context.TableB on tableA.id equals tableB.id
             where *some conditions*
             select new {
                 tableA.id,
                 tableA.name,
                 another_name = tableA.TableC.name
                 some_operation = tableB.price * tableB.TableD.some_coeficient
                 another_operation = tableB.TableE.Sum(c=> c.some_value)
             };

This is what i have right now (after this query i perform in another variable a result.ToList().

Mi question is if would be better in levels of performance to make:

  1. A query with Lambda expressions? keeping the select new {....} part.
  2. A query with the select new {....} part after the ToList()?

What do you recommend me to do?

Jon Skeet
people
quotationmark

  1. It would be exactly the same. Query expressions are basically converted to the non-query-expression equivalent.

  2. You'd need to use select new { tableA, tableB } or something similar anyway, so that you could use both variables after ToList... but it would probably be slower as then:

    • All the fields would be pulled from the database rather than only those which are used
    • The projections (including Sum etc) would all happen after fetching the values from the database, rather than within the database. This is very likely to involve making more database requests, to access tableA.TableC etc.

You should look at the generated SQL, run it in the SQL profiler to work out what's slow, consider more indexes etc.

people

See more on this question at Stackoverflow