Bring a Max (Date) with LINQ

1

I have this Linq :

var resultado = 
(
    from pdv in db.T_PDV
    from tarefa in db.T_TarefaParceiro.Where(trf => trf.CNPJ == pdv.CNPJ).DefaultIfEmpty()
    from parceiro in db.T_OsParceiro.Where(prf => prf.IDTarefaParceiro == tarefa.IDTarefaParceiro)
    from acao in db.T_Acao.Where(ac => ac.IDAcao == tarefa.IDAcao).DefaultIfEmpty()
    from proxima in db.T_ProximaAcao.Where(pxm => pxm.IDAcao == acao.IDAcao).DefaultIfEmpty()
    from info in db.T_InfoClientePdv.Where(inf => inf.CNPJ == pdv.CNPJ).DefaultIfEmpty()
    from cliente in db.T_Cliente.Where(clie => clie.IDCliente == info.IDCliente).DefaultIfEmpty()
    where pdv.CNPJ == "07599639000184"
    select new
    {
        pdv.CNPJ,
        pdv.DataCadastro,
        cliente.NomeFantasia,
        acao.Acao,
        proxima.ProximaAcao,
        parceiro.NumOs,
        parceiro.DataVisita,
        parceiro.DataAgendamento
})
.ToList()
.FirstOrDefault();

As I do in this linq, bring the result to a greater date. There is a field called DataVisita and it is for this field that I should make a Max , how do I do it?

    
asked by anonymous 13.06.2014 / 22:33

3 answers

3

One way would be: a Order By Desc catching the highest DataVisita and call the FirstOrDefault () that writes in your SQL a TOP(1) (depending on the database this can change, this SQL statement is private SQL Server):

var resultado = 
    (
        from pdv in db.T_PDV
        from tarefa in db.T_TarefaParceiro.Where(trf => trf.CNPJ == pdv.CNPJ).DefaultIfEmpty()
        from parceiro in db.T_OsParceiro.Where(prf => prf.IDTarefaParceiro == tarefa.IDTarefaParceiro)
        from acao in db.T_Acao.Where(ac => ac.IDAcao == tarefa.IDAcao).DefaultIfEmpty()
        from proxima in db.T_ProximaAcao.Where(pxm => pxm.IDAcao == acao.IDAcao).DefaultIfEmpty()
        from info in db.T_InfoClientePdv.Where(inf => inf.CNPJ == pdv.CNPJ).DefaultIfEmpty()
        from cliente in db.T_Cliente.Where(clie => clie.IDCliente == info.IDCliente).DefaultIfEmpty()
        where pdv.CNPJ == "07599639000184"
        order by parceiro.DataVisista descending
        select new
        {
            pdv.CNPJ,
            pdv.DataCadastro,
            cliente.NomeFantasia,
            acao.Acao,
            proxima.ProximaAcao,
            parceiro.NumOs,
            parceiro.DataVisita,
            parceiro.DataAgendamento
    })      
    .FirstOrDefault();
    
14.06.2014 / 15:02
2

Try the following:

var resultado = (
    from pdv in db.T_PDV
    (...)
    where pdv.CNPJ == "07599639000184" &&
    parceiro.DataVisita == (
        from parceiro2 in db.T_OsParceiro.Where(prf2 => prf2.IDTarefaParceiro == tarefa.IDTarefaParceiro)
        select parceiro2.DataVisita).Max()
    select new
    {
        pdv.CNPJ,
        pdv.DataCadastro,
        (...)
    }).ToList().FirstOrDefault();
    
13.06.2014 / 22:40
0

There is no built-in method to do this. You can use the sort logic to get the first item in the list.

You can write a generic method to get Min or Max for any type, by passing the element to be ordered:

public static TSource MaxObject<TSource, TComparator>(this IEnumerable<TSource> source,
                                                      Func<TSource, TComparator> selector)
{
   var result = source.OrderByDescending(selector);
   return result.FirstOrDefault();
}

With your use:

var maiorObjeto = lista.MaxObject(item => item.Data);
    
25.10.2018 / 17:06