(Linq) linq - join into

C#/Winform 2017. 4. 14. 11:56
728x90

People p1 = new People() { name = "A", age = 4 };
People p2 = new People() { name = "B", age = 5 };
People p3 = new People() { name = "C", age = 6 };
People p4 = new People() { name = "E", age = 7 };
People p5 = new People() { name = "F", age = 8 };

List<People> pList = new List<People>() { p1, p2, p3, p4, p5 };

 

Exam e1 = new Exam() { name = "A", subject = "과학" };
Exam e2 = new Exam() { name = "B", subject = "수학" };
Exam e3 = new Exam() { name = "A", subject = "수학" };
Exam e4 = new Exam() { name = "D", subject = "영어" };
Exam e5 = new Exam() { name = "C", subject = "기술" };
Exam e6 = new Exam() { name = "C", subject = "과학" };
List<Exam> eList = new List<Exam>() { e1, e2, e3, e4, e5, e6 };

 

var joinQuery = from pl in pList
                      join el in eList
                      on pl.name equals el.name
                      into joined
                      select new { Person = pl, Count = joined.Count() }; 

 

foreach (var grouped in joinQuery)
{
    Console.WriteLine("{0},{1}", grouped.Person.name, grouped.Count);
}

 

결과

A,2
B,1
C,2
E,0
F,0

 

728x90

'C# > Winform' 카테고리의 다른 글

(Linq) 점표기식  (0) 2017.04.14
(Linq) Cross Join  (0) 2017.04.14
(Linq) Linq to DataSet  (0) 2017.04.14
(Linq) linq to xml  (0) 2017.04.14
(Linq) linq T Type Join result T Type  (0) 2017.04.14
Posted by kjun.kr
,