선택 연산자는 투영 연산자 범주에 속하는 모든 소스 SelectMany 연산자에 대해 하나의 결과 값을 생성합니다. 시퀀스의 각 요소를 IEnumerable에 투영하고 결과 시퀀스를 하나의 시퀀스로 병합하는 데 사용됩니다.
예시
class Demo{
public string Name { get; set; }
public List<string> Contents { get; set; }
public static List<Demo>GetAllContents(){
List<Demo> listContents = new List<Demo>{
new Demo{
Name = "Cap",
Contents = new List<string> { "Nike", "Adidas" }
},
new Demo{
Name = "Shoes",
Contents = new List<string> { "Nike", "Puma", "Adidas" }
},
};
return listContents;
}
}
class Program{
static void Main(){
IEnumerable<List<string>> result = Demo.GetAllContents().Select(s => s.Contents);
foreach (List<string> stringList in result){
foreach (string str in stringList){
Console.WriteLine(str);
}
}
Console.WriteLine("---Select Many---")
IEnumerable<string> resultSelectMany = Demo.GetAllContents().SelectMany(s => s.Contents);
foreach (string str in resultSelectMany){
Console.WriteLine(str);
}
Console.ReadKey();
}
} 출력
Nike Adidas Nike Puma Adidas ---Select Many--- Nike Adidas Nike Puma Adidas