我试图比较两个列表并更新第一个列表中的值。
我的代码是
我有一个像这样的列表
ChitNo Company LoadingType BrandCode PackingCode GradeCode Quantity
1 A L B1 P1 G1 100
2 B U B1 P1 G1 90
3 C L B2 P1 G2 200
4 D L B1 P1 G1 80
我想通过BrandCode,Packing,GrandCode和LoadingType" L"此组合的数量和LoadingType" U"此组合的数量(BrandCode,PackingCode,GradeCode)。
I Excepted output is
Brand Packing Grade Quantity
B1 P1 G1 90
B2 P1 G2 200
因为我将代码作为
返回 var Loaded = (from d in LoadedList.Where(s => s.LoadingType == "L") group d by new { Brand = d.BrandCode, Packing = d.PackingCode, Grade = d.GradeCode } into g select new { Brand = g.Key.Brand, Packing = g.Key.Packing, Grade = g.Key.Grade, Quantity = g.Sum(e => e.LoadingQty) }).ToList();
var UnLoaded = (from d in LoadedList.Where(s => s.LoadingType == "U") group d by new { Brand = d.BrandCode, Packing = d.PackingCode, Grade = d.GradeCode } into g select new { Brand = g.Key.Brand, Packing = g.Key.Packing, Grade = g.Key.Grade, Quantity = g.Sum(e => e.LoadingQty) }).ToList();
但我不知道比较它和减去数量请帮助我。提前谢谢。
答案 0 :(得分:2)
foreach(var item in LoadedList.Where(r=>r.LoadingType == "U"))
{
item.Quantity *= -1;
}
var summary = from d in LoadedList
group d by new { Brand = d.BrandCode, Packing = d.PackingCode, Grade = d.GradeCode } into g
select new { Brand = g.Key.Brand, Packing = g.Key.Packing, Grade = g.Key.Grade, Quantity = g.Sum(e => e.LoadingQty) }
答案 1 :(得分:1)
var difference = (from l in Loaded
join u in UnLoaded on new { l.Brand, l.Packing, l.Grade }
equals new { u.Brand, u.Packing, u.Grade } into lu
from u in lu.DefaultIfEmpty()
select new { Brand = l.Brand, Packing = l.Packing,
Grade = l.Grade, Sum = (l.Sum - u != null ? u.Sum : 0)).ToList();