2010-11-04 45 views

回答

6
var peoplesDogs = people.SelectMany(p => p.Dogs); 
0
var peopleDogs = people.Select(p => p.Dogs) 

编辑

以上将创建一个IEnumerable<IEnumerable<Dog>>但显然我们需要的仅仅是IEnumerable<Dog>

正如LukeH的答案,你需要使用SelectMany扁平化:

var peopleDogs = people.SelectMany(p => p.Dogs) 
+0

-1出于同样的原因,约翰希恩的回答原本是错误的。这需要根据LukeH的回答和John Sheehan的回答,一般用SelectMany进行扁平化。我知道这是由于编辑问题,但目前的问题仍然存在错误 - 我建议删除它。 – 2010-11-04 22:35:10

+0

原来的问题没有说明它也是IEnumerable – 2010-11-04 22:35:57

1
var peoplesDogs = from p in people 
        from d in p.Dogs 
        select d; 
+0

他改变了问题 – 2010-11-04 22:35:01

+0

@John:够公平的......一定是快的:) – 2010-11-04 22:36:01

+0

我可以让我的两点回来吗?除非这一个也被打破,我猜测 – 2010-11-04 22:36:33

0

,你也可以做

var peoplesDogs = from p in people 
        from d in p.Dogs 
        select d; 

具有相同的效果:

var peoplesDogs = people.SelectMany(p => p.Dogs) 
相关问题