2
중첩 목록, 어떻게 람다 식으로이 작업을 수행 할 수 있습니까?
이렇게하면 작동하지 않습니다 :
// Gives me a list of List<QueryResult>
var queries = list.Select(item => item.anotherlist).ToList();
중첩 목록, 어떻게 람다 식으로이 작업을 수행 할 수 있습니까?
이렇게하면 작동하지 않습니다 :
// Gives me a list of List<QueryResult>
var queries = list.Select(item => item.anotherlist).ToList();
SelectMany 연산자는 트릭을한다고 -이 경우,리스트의 목록을 소요하고 평탄 :
var queries = list.SelectMany(sublist => sublist).ToList();
사용 selectmany
var queries = list.SelectMany(l => l.anotherList).ToList();
감사합니다. – gremo
+1. 완전성을 위해, LINQ 표현식에서'from ... from ... select ... '가 실제로'SelectMany'와 동일하다는 것을 추가 할 것입니다; '선택'으로 번역되지 않습니다. 예 : [Bart de Smet의이 쿼리 표현 번역 치트 시트] (http://bartdesmet.net/blogs/bart/archive/2008/08/30/c-3-0-query-expression-translation-cheat-sheet.aspx) 또는 [C# 3.0 사양] (http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/CSharp%20Language%20Specification.doc), 섹션 7.15.2.4 . – stakx