在每个商店国家部分中选择出生值最高的行

Pick line with the highest value of birth in every shop-country section

我已经尝试了几个小时来解决这个问题,但我无法解决
这是我在 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 }

您可以尝试在查询的最后添加 GroupBy + Select (SelectMany):

var result = E
   ...
  .Select(s => new {
     s.Key.ID, 
     s.Key.Birth, 
     ShopCont = s.Key.Shop + "-" + s.Key.Country, 
     Total    = s.Sum(ss => ss.Price)
   })
  .GroupBy(item => item.ShopCont) // we group by shop
  .Select(g => g                  // in each shop
     .OrderByDescending(item => item.Birth) // we take the latest
     .First());                             // item only   

这里我们分组 by Birth,只取最新的一个。

编辑: 如果我们可以重复 Birth 我们可以分组他们(我宁愿实现我自己的扩展方法,但在解决书中的问题时通常不允许):

  ... 
  .Select(s => new {
          s.Key.ID,
          s.Key.Birth, // Let's declare it explicitly
          ShopCont = s.Key.Shop + "-" + s.Key.Country,
          Total = s.Sum(ss => ss.Price)
        })
  .GroupBy(item => item.ShopCont) // we group by shop
  .SelectMany(outer => outer
    .GroupBy(item => item.Birth)
    .OrderByDescending(inner => inner.Key)
    .First()); 

现在我们 group by Birth(我们想要一个组,因为它可以不仅仅是一个记录到 return); OrderByDescending 后跟 First 采取正确的组,我们最终在 SelectMany

的帮助下将其压平