Possibility to use MAX in Datetime Lambda expression fields

Asked

Viewed 55 times

-1

There is the possibility to use the MAX in expression consultations lambdas, for example:

I would like to take all registration at the age of 18 years and get the only last record entered in the database, ie with the longest registration date.

Lista.FirstOrDefault(x => x.Idade.Equals(18) && x.DataCadastro);
  • You want to get two results?

  • This list is an auto field - increment ?

  • In lambda there is Max too: Lista.Max(x => x.DataCadastro), assuming the List is already filtered (age)

1 answer

1

The above solutions did not solve my problem, because I intend to bring really only one record, so I would like to use MAX, and if I use the MAX that contains in Ienumerable it brings only the result of the field that I am performing max, ie the field Datacadastro, and I need the entire list.

I solved my problem this way.

Lista.Where(x => x.Idade.Equals(18)).OrderByDescending(x => x.DataCadastro).FirstOrDefault();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.