Cómo seleccionar solo los registros con la fecha más alta en LINQ


Tengo una tabla, 'lasttraces', con los siguientes campos.

Id, AccountId, Version, DownloadNo, Date

Los datos se ven así:

28092|15240000|1.0.7.1782|2009040004731|2009-01-20 13:10:22.000
28094|61615000|1.0.7.1782|2009040007696|2009-01-20 13:11:38.000
28095|95317000|1.0.7.1782|2009040007695|2009-01-20 13:10:18.000
28101|15240000|1.0.7.1782|2009040004740|2009-01-20 14:10:22.000
28103|61615000|1.0.7.1782|2009040007690|2009-01-20 14:11:38.000
28104|95317000|1.0.7.1782|2009040007710|2009-01-20 14:10:18.000

¿Cómo puedo, en LINQ to SQL, solo obtener la última última traza de cada accountId (la que tiene la fecha más alta)?

Author: Peter Mortensen, 2009-01-22

5 answers

Si solo desea la última fecha para cada cuenta, usaría esto:

var q = from n in table
        group n by n.AccountId into g
        select new {AccountId = g.Key, Date = g.Max(t=>t.Date)};

Si quieres el registro completo:

var q = from n in table
        group n by n.AccountId into g
        select g.OrderByDescending(t=>t.Date).FirstOrDefault();
 204
Author: Mehrdad Afshari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-06 19:33:24

Aquí hay una manera sencilla de hacerlo

var lastPlayerControlCommand = this.ObjectContext.PlayerControlCommands
                                .Where(c => c.PlayerID == player.ID)
                                .OrderByDescending(t=>t.CreationTime)
                                .FirstOrDefault();

También echar un vistazo a este gran lugar LINQ - LINQ a las muestras SQL

 38
Author: Academy of Programmer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-12 13:25:28

Si quieres el registro completo, aquí hay una forma lambda:

var q = _context
             .lasttraces
             .GroupBy(s => s.AccountId)
             .Select(s => s.OrderByDescending(x => x.Date).FirstOrDefault());
 10
Author: Bob Zhang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-14 17:17:17

Podría ser algo como:

var qry = from t in db.Lasttraces
          group t by t.AccountId into g
          orderby t.Date
          select new { g.AccountId, Date = g.Max(e => e.Date) };
 4
Author: bruno conde,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-22 19:38:17

Haga esto de una manera simple: -

Se creó una clase para contener la siguiente información

  • Nivel (número)
  • Url (Url del sitio)

Vaya a la lista de sitios almacenados en un objeto ArrayList. Y ejecutado después de la consulta para ordenarlo en orden descendente por nivel.

var query = from MyClass object in objCollection 
    orderby object.Level descending 
    select object

Una vez que obtuve la colección ordenada en orden descendente, escribí el siguiente código para obtener el Objeto que viene como fila superior

MyClass topObject = query.FirstRow<MyClass>()

Esto funcionó como encanto.

 3
Author: Sudhir Kesharwani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-21 10:35:07