0

In the context of Entity Framework, which of these queries performs better?

var result = Items.OrderByDescending(t => t.Created)
                  .FirstOrDefault(a => a.ID == ItemID)?.ItemName;

or

var result = Items.Where(a => a.ID == ItemID)
                  .OrderByDescending(t => t.Created)
                  .FirstOrDefault()?.ItemName;

Is there anywhere I can read about how these LINQ queries are converted to SQL in this case.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dstewart101
  • 1,084
  • 1
  • 16
  • 38
  • 3
    Depends on the EF provider and the concrete backend database system used - but for e.g. SQL Server, I would almost bet they'd be translated into the same query and thus perform the same ..... – marc_s Nov 22 '21 at 09:11
  • @marc_s do you know where I could see how this is converted into SQL, please? – dstewart101 Nov 22 '21 at 09:40
  • 1
    See here: https://stackoverflow.com/questions/1412863/how-do-i-view-the-sql-generated-by-the-entity-framework – marc_s Nov 22 '21 at 09:41
  • 1
    Typically I opt for running a profiler to inspect the SQL actually run against the DB. This not only gives you the SQL, but also metrics on the time taken, rows touched, and can help spot issues like extra queries getting kicked off for things like lazy loading trip-ups. – Steve Py Nov 22 '21 at 21:40

0 Answers0