Bring a Max(Date) with LINQ

Asked

Viewed 2,214 times

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 Chapter, bring the result by the highest date. There is a field called DataVisita and it is by this field that I must make a Max, how I do?

  • worked out the proposed solution?

4 answers

3

A way would be, a Order By Desc catching the biggest DataVisita and call the method Firstordefault() who writes in his SQL a TOP(1) (depending on the database this may change, this SQL statement is particular to 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();

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();
  • I couldn’t get it to work. It’s giving this error: Operator '&&' cannot be Applied to operands of type 'bool' and 'System.Datetime'. I tried several ways to put parentheses in several places and even so continues to give error.

  • Adjusted. It had put '=' instead of '==' in the equality operator. Vices of VB.NET :)

0

here I take the last registration date of a watch, using the OrderBy to list from the smallest to the largest and catch the largest record date with LastOrDefautl:

var ultimo_relogio= db.Produtos.Where(p=>p.Produtos=="Relogio")
                             .OrderBy(p=>p.data_cadastro).LastOrDefault();

0

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

You can write a generic method to get the Min orMax for any type, passing by parameter 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 its use:

var maiorObjeto = lista.MaxObject(item => item.Data);

Browser other questions tagged

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