LINQ:从列表中选择项目(分组/选择/求和与最大值!)

发布于 2024-09-04 07:41:37 字数 607 浏览 4 评论 0原文

我只是沉浸在 Linq 中并享受很多乐趣!任何人都可以帮我解决这个问题吗:
我有一个数据列表:

    Key  Value
    Aaa  12
    AaA  10
    AAa  5
    BBB  2
    Bbb  1
1. I want to group by Key.ToUpper()
2. For every group I need the Max(Value) & Sum(Value)
3. For every group I want to select the entries There the Value != Max(value)
the final result should be like this:
    Key Max Total
    AaA 12  27
    AAa 12  27
    Bbb 2   3
Thanks!

更新,实际上我还需要最大条目中的密钥:

    Key Max Total Correct
    AaA 12  27    Aaa
    AAa 12  27    Aaa
    Bbb 2   3     BBB 

Just getting my head around Linq and having lots of fun! Can any one aid me with a query for this:
I have a list of data:

    Key  Value
    Aaa  12
    AaA  10
    AAa  5
    BBB  2
    Bbb  1

1. I want to group by Key.ToUpper()
2. For every group I need the Max(Value) & Sum(Value)
3. For every group I want to select the entries
There the Value != Max(value)
the final result should be like this:

    Key Max Total
    AaA 12  27
    AAa 12  27
    Bbb 2   3

Thanks!

Update, actually I also need the Key from the Maximum entry:

    Key Max Total Correct
    AaA 12  27    Aaa
    AAa 12  27    Aaa
    Bbb 2   3     BBB 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木森分化 2024-09-11 07:41:37

:)

var results =
  from kvp in source
  group kvp by kvp.Key.ToUpper() into g
  select new
  {
    Group = g,
    Max = g.Max(kvp => kvp.Value),
    Total = g.Sum(kvp => kvp.Value)
  } into ag
  from x in ag.Group  //SelectMany
  where x.Value != ag.Max
    //for the update to the question - note: possibly ambiguous
  let correct = ag.Group.Where(y => y.Value == ag.Max).First().Key
  select new
  {
    Key = x.Key,
    Max = ag.Max,
    Total = ag.Total,
    Correct = correct 
  };

我有点喜欢这个问题,因为做出答案所需的所有小部分(有些很少使用)。


Max = g.Max(kvp => kvp.Value),
Total = g.Sum(kvp => kvp.Value)

对一个组执行多个聚合很简单,但如果您不知道如何操作,则具有挑战性。


select a into b

该子句获取之前发生的所有内容并开始使用目标的新查询。如果没有它,我就必须开始一个像这样的新查询:

var A = ... select a

var B = from b in A

重要的是要注意 select into 子句从中删除了 kvpg范围。


  from b in source
  from a in b.A  //SelectMany

子集合的这种“解包”将我对 b 的查询变成了对 a 的查询。与默认的 Enumerable.SelectMany 重载不同,它将父级 (b) 保留在范围内。


where x.Value != ag.Max

将孩子的财产与父母的财产进行比较?愉快。重要的是要记住在任何想要过滤的时候都要打破where,即使你只是分组(没有HAVING)。

:)

var results =
  from kvp in source
  group kvp by kvp.Key.ToUpper() into g
  select new
  {
    Group = g,
    Max = g.Max(kvp => kvp.Value),
    Total = g.Sum(kvp => kvp.Value)
  } into ag
  from x in ag.Group  //SelectMany
  where x.Value != ag.Max
    //for the update to the question - note: possibly ambiguous
  let correct = ag.Group.Where(y => y.Value == ag.Max).First().Key
  select new
  {
    Key = x.Key,
    Max = ag.Max,
    Total = ag.Total,
    Correct = correct 
  };

I kinda like the question because of all the little parts (some are rarely used) that are required to make the answer.


Max = g.Max(kvp => kvp.Value),
Total = g.Sum(kvp => kvp.Value)

Performing multiple aggregations on a group is straightforward, yet challenging if you don't know how.


select a into b

This clause takes everything that happened before and starts a new query with the target. Without it, I'd have to start a new query like this:

var A = ... select a

var B = from b in A

It's important to note that the select into clause removes kvp and g from scope.


  from b in source
  from a in b.A  //SelectMany

This "unpacking" of the child collection turns my query about b's into a query about a's. Unlike the default Enumerable.SelectMany overload, it leaves the parent (b) in scope.


where x.Value != ag.Max

Comparing a child's property with a parent's property? Delightful. It's important to remember to break out where anytime you want to filter, even if you just grouped (there is no HAVING).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文