【发布时间】:2021-07-19 23:57:37
【问题描述】:
已经尝试了几个小时来解决问题,但我无法解决问题
这是我在pastebin 上使用的表格
var result = E
.Join(A, a => a.ID, aa => aa.ID,
(EE, AA) => new {AA.ID, AA.Birth, AA.Street, EE.Code, EE.Shop})
.Join(D, d => d.Code, dd => dd.Code,
(AE, DD) => new {AE.ID, AE.Birth, AE.Street, AE.Shop, DD.Code, DD.Price})
.Join(B, b => b.Code, bb => bb.Code,
(AED, BB) => new {AED.ID, AED.Birth, AED.Shop, AED.Price, BB.Country})
.GroupBy(g => new
{
g.ID, g.Birth, g.Shop, g.Country
})
.Select(s => new
{
s.Key.ID, s.Key.Birth,
ShopCont = s.Key.Shop + "-" + s.Key.Country,
Total = s.Sum(ss => ss.Price)
});
这就是结果的样子
{ ID = 1, Birth = 1992, ShopCont = Gucci-Nigeria, Total = 64 }
{ ID = 2, Birth = 2001, ShopCont = Gucci-Russia, Total = 41 }
{ ID = 3, Birth = 1998, ShopCont = Gucci-Russia, Total = 123 } // this should be removed
{ ID = 3, Birth = 1998, ShopCont = Dior-Russia, Total = 32 }
{ ID = 4, Birth = 2003, ShopCont = Dior-USA, Total = 23 }
{ ID = 1, Birth = 1992, ShopCont = Adidas-USA, Total = 1290 }
{ ID = 1, Birth = 1992, ShopCont = Adidas-Germany, Total = 321 }
{ ID = 5, Birth = 2005, ShopCont = Dixi-Germany, Total = 4 }
{ ID = 5, Birth = 2005, ShopCont = Dixi-France, Total = 1890 }
{ ID = 4, Birth = 2003, ShopCont = Dixi-France, Total = 1695 } // this should be removed
我想看看这个
{ ID = 1, Birth = 1992, ShopCont = Gucci-Nigeria, Total = 64 }
{ ID = 2, Birth = 2001, ShopCont = Gucci-Russia, Total = 41 }
{ ID = 3, Birth = 1998, ShopCont = Dior-Russia, Total = 32 }
{ ID = 4, Birth = 2003, ShopCont = Dior-USA, Total = 23 }
{ ID = 1, Birth = 1992, ShopCont = Adidas-USA, Total = 1290 }
{ ID = 1, Birth = 1992, ShopCont = Adidas-Germany, Total = 321 }
{ ID = 5, Birth = 2005, ShopCont = Dixi-Germany, Total = 4 }
{ ID = 5, Birth = 2005, ShopCont = Dixi-France, Total = 1890 }
【问题讨论】:
-
为什么你认为应该删除这些行?它们不是重复的。他们是不同的年份。
-
是的,你是对的,但目的是在每个商店国家部分中选择出生价值最高的路线。这是书中的练习
-
执行 OrderByDescending(x => x.Total) 然后执行 GroupBy(x => x.ShopCont) 最后从每个组中选择第一个 .Select(x => x.First())
标签: c# linq linq-to-objects