Possibility to use MAX in fields DateTime expression Lambda

-1

There is the possibility of using MAX in expression queries lambdas , for example:

I would like to get all the records at the age of 18 and get the only last record inserted in the database, that is, with the longest registration date.

Lista.FirstOrDefault(x => x.Idade.Equals(18) && x.DataCadastro);
    
asked by anonymous 06.04.2018 / 16:04

1 answer

1

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

I solved my problem as follows.

Lista.Where(x => x.Idade.Equals(18)).OrderByDescending(x => x.DataCadastro).FirstOrDefault();
    
06.04.2018 / 16:14