大型 LINQ 分组查询,幕后发生的事情
以以下 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();
- 为了计算每个值,“Ninjas”列表被迭代了多少次?
- 使用 foreach 循环来加速此类查询的执行会更快吗?
- 在 Ninjas 之后添加 '.AsParallel()' 会带来性能提升吗?
- 有没有更好的方法来计算 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();
- How many times is the list of 'Ninjas' being iterated over in order to calculate each of the values?
- Would it be faster to employ a foreach loop to speed up the execution of such a query?
- Would adding '.AsParallel()' after Ninjas result in any performance improvements?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设这是一个 LINQ to Objects 查询:
Ninjas
仅迭代一次;这些组被构建到内部具体列表中,然后您可以对其进行多次迭代(每个聚合一次)。AsParallel
可能会加快速度 - 它看起来很容易并行化。值得一试...您可能想查看
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).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 :)AsParallel
might speed things up - it looks pretty easily parallelizable. Worth a try...You might want to have a look at the
GroupBy
post in my Edulinq blog series for more details on a possible implementation.