0

I have this line of code (using MSSQL):

double avg = db.MyTable.Where(x => x.RecordDate < today).Select(z => z.value).Average();

I wonder if this is translated to SQL AVG() function or the Average() run on client side ?

This table contains 50,000 records per day and I prefer to let the database calculate the average and pass back only a single value.

And, how can I see the SQL query sent to the database ?

Ofiris
  • 6,047
  • 6
  • 35
  • 58

1 Answers1

1

If you don't enumerate your queryable, yes, average will be done on db side.

You can also simplify your code. Select is not needed, there's an overload for average taking an Expression<Func<T, double>> (or a decimal, int...) as parameter.

double avg = db.MyTable.Where(x => x.RecordDate < today).Average(z => z.value);

and they are quite many ways to see the sql generated, you can google for sql generated linq, or take a look here, for example

Community
  • 1
  • 1
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122