C# SelectMany 三层深
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/640415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SelectMany Three Levels Deep
提问by blu
I can flatten the results of a child collection within a collection with SelectMany:
我可以使用 SelectMany 将集合中的子集合的结果展平:
// a list of Foos, a Foo contains a List of Bars
var source = new List<Foo>() { ... };
var q = source.SelectMany(foo => foo.Bar)
.Select(bar => bar.barId)
.ToList();
this gives me the list of all Bar Ids in the Foo List. When I attempt to go three levels deep the incorrect result is returned.
这给了我 Foo 列表中所有 Bar Id 的列表。当我尝试深入三层时,返回了错误的结果。
var q = source.SelectMany(foo => foo.Bar)
.SelectMany(bar => bar.Widget)
.Select(widget => widget.WidgetId)
.ToList();
How should I be using SelectMany to get the list of all Widgets in all Bars in my list of Foos?
我应该如何使用 SelectMany 来获取我的 Foos 列表中所有栏中的所有小部件的列表?
EditI miss-worded the above sentence, but the code reflects the goal. I am looking for a list of all Widget Ids, not widgets.
编辑我写错了上面的句子,但代码反映了目标。我正在寻找所有小部件 ID 的列表,而不是小部件。
An "incorrect" result is not all of the widget ids are returned.
“不正确”的结果不是所有的小部件 ID 都被返回。
采纳答案by Jon Skeet
Your query is returning all the widget IDs, instead of all the widgets. If you just want widgets, just use:
您的查询将返回所有小部件 ID,而不是所有小部件。如果您只想要小部件,只需使用:
var q = source.SelectMany(foo => foo.Bar)
.SelectMany(bar => bar.Widget)
.ToList();
If that's still giving "the incorrect result" please explain in what wayit's the incorrect result. Sample code would be very helpful :)
如果仍然给出“不正确的结果”,请以何种方式解释不正确的结果。示例代码会很有帮助:)
EDIT: Okay, if you want the widget IDs, your original code should be fine:
编辑:好的,如果你想要小部件 ID,你的原始代码应该没问题:
var q = source.SelectMany(foo => foo.Bar)
.SelectMany(bar => bar.Widget)
.Select(widget => widget.WidgetId)
.ToList();
That could also be written as
也可以写成
var q = (from foo in source
from bar in foo.Bar
from widget in bar.Widget
select widgetId).ToList();
if you like query expression format.
如果您喜欢查询表达式格式。
This really should work - if it's notworking, that suggests there's something wrong with your data.
这确实应该努力-如果它不工作,这表明有什么东西不对您的数据。
We should have checked before - is this just LINQ to Objects, or a fancier provider (e.g. LINQ to SQL)?
我们之前应该检查过 - 这只是 LINQ to Objects,还是更高级的提供程序(例如 LINQ to SQL)?
回答by eglasius
var q = (
from f in foo
from b in f.Bars
from w in b.Widgets
select w.WidgetId
).ToList();
Also note that if you want the unique list, you can do .Distinct().ToList() instead.
另请注意,如果您想要唯一列表,则可以执行 .Distinct().ToList() 。
回答by Abdalwhab Bakheet
var q = source.SelectMany(foo => foo.Bar)
.SelectMany(bar => bar.Widget,(bar,widget) => widget.WidgetId)
.ToList();
we can call this overload of SelectMany() with allow us to specify the projection using lambda experession
我们可以调用 SelectMany() 的重载,并允许我们使用 lambda 表达式指定投影