大型 LINQ 分组查询,幕后发生的事情

发布于 2024-12-04 03:46:04 字数 1007 浏览 2 评论 0原文

以以下 LINQ 查询为例。请不要评论代码本身,因为我刚刚输入它是为了帮助解决这个问题。

以下 LINQ 查询使用“分组依据”并计算摘要信息。正如您所看到的,正在对数据执行大量计算,但 LINQ 在幕后的效率如何。

var NinjasGrouped = (from ninja in Ninjas 
    group pos by new { pos.NinjaClan, pos.NinjaRank } 
    into con 
    select new NinjaGroupSummary 
    { 
        NinjaClan = con.Key.NinjaClan, 
        NinjaRank = con.Key.NinjaRank, 
        NumberOfShoes = con.Sum(x => x.Shoes), 
        MaxNinjaAge = con.Max(x => x.NinjaAge), 
        MinNinjaAge = con.Min(x => x.NinjaAge), 
        ComplicatedCalculation = con.Sum(x => x.NinjaGrade) != 0 
        ? con.Sum(x => x.NinjaRedBloodCellCount)/con.Sum(x => x.NinjaDoctorVisits)
        : 0,
    ListOfNinjas = con.ToList() 
    }).ToList(); 
  1. 为了计算每个值,“Ninjas”列表被迭代了多少次?
  2. 使用 foreach 循环来加速此类查询的执行会更快吗?
  3. 在 Ninjas 之后添加 '.AsParallel()' 会带来性能提升吗?
  4. 有没有更好的方法来计算 List 的汇总信息?

当我们在整个软件中使用这种类型的代码时,任何建议都会受到赞赏,我真的很想更好地了解 LINQ 在幕后所做的事情(可以这么说)。也许有更好的方法?

Take the following LINQ query as an example. Please don't comment on the code itself as I've just typed it to help with this question.

The following LINQ query uses a 'group by' and calculates summary information. As you can see there are numerous calculations which are being performed on the data but how efficient is LINQ behind the scenes.

var NinjasGrouped = (from ninja in Ninjas 
    group pos by new { pos.NinjaClan, pos.NinjaRank } 
    into con 
    select new NinjaGroupSummary 
    { 
        NinjaClan = con.Key.NinjaClan, 
        NinjaRank = con.Key.NinjaRank, 
        NumberOfShoes = con.Sum(x => x.Shoes), 
        MaxNinjaAge = con.Max(x => x.NinjaAge), 
        MinNinjaAge = con.Min(x => x.NinjaAge), 
        ComplicatedCalculation = con.Sum(x => x.NinjaGrade) != 0 
        ? con.Sum(x => x.NinjaRedBloodCellCount)/con.Sum(x => x.NinjaDoctorVisits)
        : 0,
    ListOfNinjas = con.ToList() 
    }).ToList(); 
  1. How many times is the list of 'Ninjas' being iterated over in order to calculate each of the values?
  2. Would it be faster to employ a foreach loop to speed up the execution of such a query?
  3. Would adding '.AsParallel()' after Ninjas result in any performance improvements?
  4. Is there a better way of calculating summery information for List?

Any advice is appreciated as we use this type of code throughout our software and I would really like to gain a better understanding of what LINQ is doing underneath the hood (so to speak). Perhaps there is a better way?

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

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

发布评论

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

评论(1

青柠芒果 2024-12-11 03:46:04

假设这是一个 LINQ to Objects 查询:

  • Ninjas 仅迭代一次;这些组被构建到内部具体列表中,然后您可以对其进行多次迭代(每个聚合一次)。
  • 使用 foreach 循环几乎肯定不会加快速度 - 您可能会从缓存一致性中受益更多(因为每次迭代一个组时,它可能必须从更高级别获取数据缓存或主内存),但我非常怀疑它是否重要。不过,实现它时的痛苦增加可能很重要:)
  • 使用AsParallel可能会加快速度 - 它看起来很容易并行化。值得一试...
  • 说实话,对于 LINQ to Objects 来说没有更好的方法了。如果能够在分组时执行聚合,那就太好了,响应式扩展将允许您执行类似的操作,但目前这可能是最简单的方法。

您可能想查看 GroupBy 在我的 Edulinq 博客系列中发帖,了解有关可能实现的更多详细信息。

Assuming this is a LINQ to Objects query:

  • Ninjas is only iterated over once; the groups are built up into internal concrete lists, which you're then iterating over multiple times (once per aggregation).
  • Using a foreach loop almost certainly wouldn't speed things up - you might benefit from cache coherency a bit more (as each time you iterate over a group it'll probably have to fetch data from a higher level cache or main memory) but I very much doubt that it would be significant. The increase in pain in implementing it probably would be significant though :)
  • Using AsParallel might speed things up - it looks pretty easily parallelizable. Worth a try...
  • There's not a much better way for LINQ to Objects, to be honest. It would be nice to be able to perform the aggregation as you're grouping, and Reactive Extensions would allow you to do something like that, but for the moment this is probably the simplest approach.

You might want to have a look at the GroupBy post in my Edulinq blog series for more details on a possible implementation.

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