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());
You can get the largest amount in ddr.Dil
using IEnumerable
's Max function.
Also note that the documentation on ForEach says:
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
and replace the 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));