选择许多三层深层 [英] SelectMany Three Levels Deep

查看:81
本文介绍了选择许多三层深层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过SelectMany将集合中的子集合的结果展平:

  //一系列Foos,a Foo包含一个列表
var source = new List< Foo>(){...};

var q = source.SelectMany(foo => foo.Bar)
.Select(bar => bar.barId)
.ToList();

这给了我Foo List中所有Bar Ids的列表。当我尝试深入三层时,会返回不正确的结果。

  var q = source.SelectMany(foo => foo .bar)
.SelectMany(bar => bar.Widget)
.Select(widget => widget.WidgetId)
.ToList();

我应该如何使用SelectMany获取我列表中的所有酒吧中所有小工具的列表?

编辑
我错过了上述句子,但代码反映了目标。我正在寻找所有Widget ID的列表,而不是Widget。



一个不正确的结果并不是所有的widget id都被返回。

解决方案

您的查询将返回所有窗口小部件ID,而不是所有窗口小部件。如果您只想要小部件,只需使用:

  var q = source.SelectMany(foo => foo.Bar)
.SelectMany(bar => bar.Widget)
.ToList();

如果仍然给出不正确的结果,请以方式这是不正确的结果。示例代码将非常有用:)编辑:好吧,如果你想要的部件ID,你的原始代码应该是罚款:

  var q = source.SelectMany(foo => foo.Bar)
.SelectMany(bar => bar.Widget)
。选择(widget => widget.WidgetId)
.ToList();

也可以写成

  var q =(源自源foo中的foo 
中的bar中的
bar.Widget中的小部件

选择widgetId).ToList();

如果您喜欢查询表达式格式。



这真的应该工作 - 如果它不是工作,这表明你的数据有问题。



我们应该检查过 - 这是只是LINQ到对象,或者一个更好的提供者(例如LINQ to SQL)?

I can flatten the results of a child collection within a collection with 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.

 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?

Edit I miss-worded the above sentence, but the code reflects the goal. I am looking for a list of all Widget Ids, not widgets.

An "incorrect" result is not all of the widget ids are returned.

解决方案

Your query is returning all the widget IDs, instead of all the widgets. If you just want widgets, just use:

var q = source.SelectMany(foo => foo.Bar)
              .SelectMany(bar => bar.Widget)
              .ToList();

If that's still giving "the incorrect result" please explain in what way it's the incorrect result. Sample code would be very helpful :)

EDIT: Okay, if you want the widget IDs, your original code should be fine:

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 not working, 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)?

这篇关于选择许多三层深层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆