如何使用 LINQ 避免嵌套循环?
我一直在阅读有关 LINQ to Objects 的内容,现在我的同事希望我向他们介绍它。
现在,我对运算符和语法选择有了一定的了解,但我听说您可以通过使用 LINQ 来避免大量嵌套循环。不过,我很难想出一组好的“之前和之后”代码清单来证明这一点。
我在 Magennis 的书中找到了使用和不使用 LINQ 进行排序和分组的一个很好的示例,他还提供了一个编写 xml 的示例。但是那些嵌套循环呢?考虑到我们通常需要一两个 foreach 循环来迭代查询结果,这是否是一个现实的主张?
如果有人可以向我解释这个想法(最好有具体的例子),我将不胜感激。
I've been reading about LINQ to Objects, and now my colleagues want me to present it to them.
Now, I have an OK understanding of the operators and the syntax choices, but I've heard you can avoid heavy nested loops by using LINQ. I'm having trouble coming up with a good set of "before and after" code listings to demonstrate this though.
I found a great example of sorting and grouping with and without LINQ in Magennis' book, and he also has an example of writing xml. But what about those nested loops? Is this even a realistic claim, given that we usually need a foreach
loop or two to iterate over the results of the query anyway?
If anyone can explain this idea to me (ideally with specific examples), I would greatly appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一种可以使用 Linq 删除的嵌套循环。
它可以变成:
使用
SelectMany
IEnumerable
上的
扩展方法。这非常有用的一个地方是嵌套循环双断点场景。
Here's a type of nested loop you can remove with Linq.
It can be turned into:
Using the
SelectMany
extension method onIEnumerable
.One place where this is quite useful is for nested loop double-break scenarios.
假设您有很多产品,例如:
并且您想查找所有价值 > 的产品。 $100.0,按类别分组,您可以使用
foreach
执行此操作:或者,您可以简单地使用 LINQ 方法:
或使用 LINQ 表达式语法:
更加简洁且不易出错。
Say you've got a lot of products, such as:
And you want to find all products with a value > $100.0, grouped by category, you could do this using
foreach
:Or, you could simply use the LINQ methods:
Or using LINQ expression syntax:
Much more concise and less error-prone.
可能是:
could be:
这是一个有点人为的例子。
假设给您一个字符串列表,您的任务是在
HashSet
中查找并返回这些字符串中找到的所有控制字符。你可以这样做:
或者使用 LINQ:
Here's a somewhat contrived example.
Suppose you were given a list of strings and your task was to find and return all the control characters found in those strings in a
HashSet<>
.You might do something like this:
Or using LINQ:
最有用的示例是当您可以使用 LINQ 中的内置方法时,例如
All
和Any
。像这样:用带有 if 和 break 的 for 循环以及 bool 检查变量编写它,我想至少需要五行代码才能完成相同的操作。嗯,让我们看看:
哎呀,9 行。并且您需要仔细阅读其中至少三篇才能了解代码的作用。
嗯,更多的是相同的。假设哺乳动物有真实的类型层次结构。
这将返回所有可以铸造成
Cat
的动物,并返回它们,铸造并准备使用。用循环编写:说实话,您应该将其分解为一个单独的方法,并使用
yield return cat;
来获得与 LINQ 版本相同的惰性行为。但我更喜欢查询语法。读起来很流畅,没有什么干扰。
再次用普通循环编写,
需要一个带有yield return的单独方法来获得相同的惰性求值行为。
The most useful examples are when you can use the built in methods in LINQ, like
All
andAny
. Like this:Write that with a for loop with if and break and a bool check variable, I guess that would be at least five lines of code to do the same. Hmm, lets see:
ooops, 9 lines. And you need to read at least three carefully of them to know what the code does.
Well, more of the same. Assuming the mammals have a real type hierarchy.
This returns all animals that can be casted into a
Cat
and returns them, casted and ready to use. Written with loops:To be honest you should break that out to a separate method and use
yield return cat;
to get the same lazy behaviour as the LINQ version.But I prefer the query syntax. It is nice and fluent to read with very little noice.
Written with plain loops
again a separate method with
yield return
would be required to get the same lazy evaluation behavior.