Select Where Table.Value == Maximum

I have an SQL table with the following design:

TableSchedule:
Id
Description
ImportedDate

I can import a large number of elements, and all of them will have the same ImportedDate.

How can I write a LINQ query that only captures records with the latest ImportedDate?

var ResultSchedule =
    from a in Db.TableSchedule
    where a.ImportedDate == (Newest?)
+3
source share
5 answers

try it

var ResultSchedule = 
from a in Db.TableSchedule 
where a.ImportedDate == (from item in DB.TableSchedule Select item.ImportedDate).Max() 
+1
source

Try the following:

  Select * From Table
  Where ImportedDate  = 
     (Select max(ImportedDate) From Table)

If you need the last entry for each [something], for example, for each individual client, then do a siubquery correlation

  Select * From Table t
  Where ImportedDate  = 
     (Select max(ImportedDate) 
      From Table
      Where Customer = t.Customer)
0
source

:

var ResultSchedule = from a in Db.TableSchedule
                     let max = Db.TableSchedule.Max(i => i.ImportedDate)
                     where a.ImportedDate == max
                     select a;
0

:

var ResultsSchedule = Db.TableSchedule.
                      GroupBy(a => a.ImportedDate).
                      OrderByDescending(g => g.Key).
                      FirstOrDefault().
                      Select(g => g);
0

:

var ResultSchedule = 
    Db.TableSchedule.Where(
       a => a.ImportedDate.Equals(Db.TableSchedule.Max(b => b.ImportedDate))
    );
0

Source: https://habr.com/ru/post/1768294/


All Articles