从IEnumerable <ienumerable <object>&gt;中选择IEnumerable <object>。 LINQ </IEnumerable的<对象> </object>

时间:2013-09-13 10:05:32

标签: c# .net linq

我需要做的是选择嵌套元素列表,这是我的查询returns IEnumerable<IEnumerable<object>>这里是我的linq表达式:

from a in (questions.Select(x => x.AnswerList).ToList())
                           select a.Select(x => x.AnswerBasicViewModel);

我应该如何做到return而不是IEnumerable<object>而不是IEnumerable<IEnumerable<object>>

为了清楚我的样本,我想得到IEnumerable<AnswerBasicViewModel>

2 个答案:

答案 0 :(得分:8)

使用SelectMany运算符:

from q in questions
from a in q.AnswerList
select a.AnswerBasicViewModel

或者只是

questions.SelectMany(q => q.AnswerList)
         .Select(a => a.AnswerBasicViewModel)

答案 1 :(得分:3)

SelectMany是这里的方式:

questions.SelectMany(x => x.AnswerList).Select(x => x.AnswerBasicViewModel);