No puedo obtener el registro más alto de una lista

I have two lists, ddr.Out and ddr.Dil. The first list I have to group by contract and then order descending by contract and then by amount since there can be several records with the same contract and I want just the one with the highest amount.

Then I have to update each amount in ddr.out with the amount from contracts that exist in ddr.dil.

My prolem is that I can't fit in this statement a way to get just the highest amount from ddr.out.

Any ideas? Rui Martins

ddr.Out.GroupBy(ou => ou.Contract);
ddr.Out.OrderByDescending(ou => ou.Contract).ThenByDescending(ou=> ou.Amount);
ddr.Out.ForEach(ou => ou.Amount += ddr.Dil
    .Where(dil => dil.Referred && 
                  dil.Wad == wad &&
                  dil.Cycle == cal.ID && 
                  dil.Contract == ou.Contract)
    .Select(dil => dil.Amount)
    .FirstOrDefault());

preguntado el 28 de agosto de 12 a las 12:08

1 Respuestas

You can get the largest amount in ddr.Dil usar IEnumerable's Funcion max.

Also note that the documentation on Para cada dice:

Modifying the underlying collection in the body of the Action<T> delegate 
is not supported and causes undefined behavior.

Rather make a new collection using Select y reemplazar el ddr.Out:

newOut = 
    ddr.Out.GroupBy(ou => ou.Contract)
       .OrderByDescending(ou => ou.Contract)
       .ThenByDescending(ou=> ou.Amount)
       .Select(ou => ou.Amount += ddr.Dil
           .Where(dil => dil.Referred && 
                  dil.Wad == wad &&
                  dil.Cycle == cal.ID && 
                  dil.Contract == ou.Contract)
           .Max(dil => dil.Amount));

Respondido 28 ago 12, 12:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.