我需要做的是选择嵌套元素列表,这是我的查询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>
。
答案 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);