Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在每个店铺所在国家/地区选择出生值最高的行_C#_Linq_Linq To Objects - Fatal编程技术网

C# 在每个店铺所在国家/地区选择出生值最高的行

C# 在每个店铺所在国家/地区选择出生值最高的行,c#,linq,linq-to-objects,C#,Linq,Linq To Objects,我已经试了几个小时来解决这个问题,但我无法解决 这是我正在处理的表格 这就是结果的样子 { 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

我已经试了几个小时来解决这个问题,但我无法解决
这是我正在处理的表格

这就是结果的样子

{ 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   
这里我们按出生分组,只取最近的一个

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


现在我们按出生分组(我们想要一个分组,因为它可以不止返回一条记录);使用降序
OrderByDescending
后接
First
选择正确的组,我们最终通过
选择多个

将其展平。您认为为什么应该删除这些行?它们不是重复的。他们是不同的年份。是的,你说得对,但目的是在每个商店所在国家/地区挑选出生价值最高的产品线。这是本书中的练习,先执行OrderByDescending(x=>x.Total),然后执行GroupBy(x=>x.ShopCont),最后从每个组中选择first。select(x=>x.first())[CS0411]方法“Enumerable.SelectMany(IEnumerable,Func)”的类型参数不能从用法中推断出来。请尝试显式指定类型参数。@Invi:对不起,您只需要整个组中的一项(最新的一项);因为我们只有一个项目,所以没有什么可展平的,也不需要
SelectMany
(但是
Select
),但是如果不是只有一个最高值,那么我们需要将它们全部取下。如果我们有两个或两个以上相同
ShopCont
Birth
的记录,我应该怎么办?是的,你说得对
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   
  ... 
  .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());