(C#동영상)C# LINQ 링크/링큐 Select, SelectMany 비교, 차이점 실습, 닷넷교육동영상, C#학원, 닷넷학원, 자바학원, WPF학원, 닷넷교육, C#교육. 자바교육
http://ojc.asia/bbs/board.php?bo_table=LecCsharp&wr_id=420
ojc.asia
https://www.youtube.com/watch?v=GqDWbAOjO4A&list=PLxU-iZCqT52DJyR6gqJy0MCL8RiTVXdos&index=22


https://www.youtube.com/watch?v=Sp_WKTMGJ8U&list=PLxU-iZCqT52DJyR6gqJy0MCL8RiTVXdos&index=25
LINQ
Select, SelectMany
실습
C# LINQ(Select, SelectMany)
LINQ에서 프로젝션 작업은 필요한 속성을 가지고 오는 것인데 Select, SelectMany를 사용한다.
기본적으로 Select는 하나의 컬렉션에서 추출하는 것이고, SelectMany는 다중 컬렉션에서 질의가 가능하다. 즉 하나의 컬렉션 안에 또 다른 SUB 컬렉션이 있을 때 SUB 컬렉션에서 데이터를 추출하는 경우에 사용한다.
또한 SelectMany는 SQL의 Cross Join과 같이 가능한 모든 조인 결과 집합을 만들어 낼 수 있다.
namespace SemectMany
{
class Customer
{
public string Name { get; set; }
public List<String> Cards { get; set; }
}
class Test
{
static List<Customer> Getcustomers()
{
List<Customer> customers = new List<Customer>{
new Customer {
Name = "박길동",
Cards= new List<string>{"BC","HANA", "WOORI"}},
new Customer {
Name = "김길동",
Cards= new List<string>{"BC","WOORI"}},
new Customer {
Name = "가길동",
Cards= new List<string>{"HANA","WOORI"}},
new Customer {
Name = "하길동",
Cards= new List<string>{"WOORI","BC"}}};
return customers;
}
static void Main(string[] args)
{
List<string> animals = new List<string>() { "cat", "dog", "monkey" };
List<int> number = new List<int>() { 10, 20 };
// SQL의 Cross Join과 같이 가능한 모든 조인 집합을 만듦
var mix1 = number.SelectMany(num => animals, (n, a) => new { n, a }).ToList();
mix1.ForEach(n => Console.WriteLine(n));
Console.WriteLine("-------------1");
// 다중 컬렉션 질의
var mix2 = animals.SelectMany(a => a).ToList();
mix2.ForEach(n => Console.WriteLine(n));
Console.WriteLine("-------------2");
// 단일컬렉션 질의
var animals1 = animals
.Where(n => n.StartsWith("c"))
.Select(n => n).ToList();
animals1.ForEach(n => Console.WriteLine(n));
Console.WriteLine("-------------3");
// 다중컬렉션 질의
var animals2 = animals
.Where(n => n.StartsWith("c"))
.SelectMany(n => n).ToList();
animals2.ForEach(n => Console.WriteLine(n));
Console.WriteLine("-------------4");
// customers에서 이름이 '가'로 시작되는 고객의 이름출력
var result1 = from c in Getcustomers()
where c.Name.StartsWith("가")
select c.Name;
result1.ToList().ForEach(n => Console.WriteLine(n));
Console.WriteLine("-------------5");
//new로 만들어내는 무명개체에서 Name추출
var result2 = from c in Getcustomers()
where c.Name.StartsWith("가")
select new { c.Name };
result2.ToList().ForEach(n => Console.WriteLine(n));
Console.WriteLine("-------------6");
//Select로 Customer의 Card를 출력, foreach문 2개필요
//바깥쪽 반복문에서 개인별 카드리스트를 출력
//안쪽 반복문에서 카드명을 출력
var result3 = Getcustomers().Select(c => c.Cards);
foreach (List<String> cardList in result3)
{
foreach (string card in cardList)
{
Console.WriteLine(card);
}
}
Console.WriteLine("-------------7");
// 메소드기반 쿼리식에서 SelectMany를 직접사용
// 다중 컬렉션 질의
var result4 = Getcustomers().SelectMany(c => c.Cards);
result4.ToList().ForEach(n => Console.WriteLine(n));
Console.WriteLine("-------------8");
// 메소드기반 쿼리식에서 Where를 이용하여 BC카드를 가진 Customer의 Name을 출력
var result5 = Getcustomers()
.Where(c => c.Cards.Contains("BC"))
.Select(c => c.Name);
result5.ToList().ForEach(n => Console.WriteLine(n));
Console.WriteLine("-------------9");
// 메소드기반 쿼리식에서 Customer.Name별 Card를 출력
var result6 = Getcustomers()
.SelectMany(cust => cust.Cards,
(cust, card) => new { cust.Name, card });
result6.ToList().ForEach(n => Console.WriteLine(n));
Console.WriteLine("-------------10");
}
}
}
#LINQ, #링크, #링큐, #Select, #SelectMany, #닷넷교육, #시샵교육, #닷넷동영상, #시샵동영상, LINQ, 링크, 링큐, Select, SelectMany, 닷넷교육, 시샵교육, 닷넷동영상, 시샵동영상,