728x90
반응형
Linq에서 projection은 지속 사용할 properties만 담긴 새로운 object를 생성하는 작업이다.
projection operations
1. Select
2. SelectMany
class Bouquet
{
public List<string> Flowers { get; set; }
}
static void SelectVsSelectMany()
{
List<Bouquet> bouquets = new()
{
new Bouquet { Flowers = new List<string> { "sunflower", "daisy", "daffodil", "larkspur" }},
new Bouquet { Flowers = new List<string> { "tulip", "rose", "orchid" }},
new Bouquet { Flowers = new List<string> { "gladiolis", "lily", "snapdragon", "aster", "protea" }},
new Bouquet { Flowers = new List<string> { "larkspur", "lilac", "iris", "dahlia" }}
};
IEnumerable<List<string>> query1 = bouquets.Select(bq => bq.Flowers);
IEnumerable<string> query2 = bouquets.SelectMany(bq => bq.Flowers);
Console.WriteLine("Results by using Select():");
// Note the extra foreach loop here.
foreach (IEnumerable<String> collection in query1)
foreach (string item in collection)
Console.WriteLine(item);
Console.WriteLine("\nResults by using SelectMany():");
foreach (string item in query2)
Console.WriteLine(item);
/* This code produces the following output:
Results by using Select():
sunflower
daisy
daffodil
larkspur
tulip
rose
orchid
gladiolis
lily
snapdragon
aster
protea
larkspur
lilac
iris
dahlia
Results by using SelectMany():
sunflower
daisy
daffodil
larkspur
tulip
rose
orchid
gladiolis
lily
snapdragon
aster
protea
larkspur
lilac
iris
dahlia
*/
}
728x90
반응형
'C#' 카테고리의 다른 글
C#] Benefits of Polymorphism, 다형성 장점 (0) | 2022.07.27 |
---|---|
C#] Inheritance, Polymorphism, Abstraction, Interface (0) | 2022.07.21 |
C#] 반복자(Iterator), yield keyword (0) | 2022.07.19 |
C#] Delegates, Anonymous Methods and Lambda Expressions (0) | 2022.07.18 |
C#] DynamicObject Class VS ExpandoObject Class (0) | 2022.07.07 |
댓글