¿Cómo obtener el valor máximo de una columna usando Entity Framework?


Para obtener el valor máximo de una columna que contiene entero, puedo usar el siguiente comando T-SQL

SELECT MAX(expression )
FROM tables
WHERE predicates;

Es posible obtener el mismo resultado con Entity Framework.

Digamos que tengo el siguiente modelo

public class Person
{
  public int PersonID { get; set; }
  public int Name { get; set; }
  public int Age { get; set; }
}

¿Cómo obtengo la edad de la persona mayor?

int maxAge = context.Persons.?
Author: abatishchev, 2011-09-25

7 answers

Prueba esto int maxAge = context.Persons.Max(p => p.Age);

Y asegúrese de que tiene using System.Linq; en la parte superior de su archivo

 106
Author: krolik,
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
2015-11-23 19:08:23

Si la lista está vacía obtengo una excepción. Esta solución tendrá en cuenta este problema:

int maxAge = context.Persons.Select(p => p.Age).DefaultIfEmpty(0).Max();
 24
Author: Carlos Toledo,
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-12-10 07:01:30

O puedes probar esto:

(From p In context.Persons Select p Order By age Descending).FirstOrDefault
 7
Author: DaniCode,
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
2013-05-03 14:02:44
maxAge = Persons.Max(c => c.age)

O algo por el estilo.

 5
Author: E.J. Brennan,
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
2015-03-20 12:21:37

Tal vez ayude, si desea agregar algún filtro:

context.Persons
.Where(c => c.state == myState)
.Select(c => c.age)
.DefaultIfEmpty(0)
.Max();
 4
Author: Foy,
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-10-02 13:50:15

En VB.Net sería

Dim maxAge As Integer = context.Persons.Max(Function(p) p.Age)
 2
Author: dipi evil,
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-05-29 16:20:50

Como muchos dijeron-esta versión

int maxAge = context.Persons.Max(p => p.Age);

Lanza una excepción cuando la tabla está vacía.

Use

int maxAge = context.Persons.Max(x => (int?)x.Age) ?? 0;

O

int maxAge = context.Persons.Select(x => x.Age).DefaultIfEmpty(0).Max()
 0
Author: A K,
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
2018-06-26 16:23:38