반응형
수량자, 생성 관련한 컬렉션 메소드에 대해 알아봅니다.
메서드
메서드 이름 | 설명 | C# 쿼리 식 구문 |
Join | 키를 기준으로 두 시퀀스를 Join한 다음 값 추출 | join … in … on … equals … |
GroupJoin | 키를 기준으로 두 시퀀스를 Join한 다음 결과를 그룹화 | join … in … on … equals … into … |
GroupBy | 공통 특성을 공유하는 요소를 그룹화 | group … by group … by … into … |
ToLookup | 키 선택기 함수에 따라 Lookup<TKey,TElement>(일대다 사전)에 요소를 삽입 | |
Aggregate | 컬렉션 값에 대해 사용자 지정 집계 작업을 수행 | |
Average | 평균 값 | |
Count | 수량 | |
Max | 최대값 | |
Min | 최소값 | |
Sum | 합계 |
Join
키가 같은지 여부에 따라 두 데이터 소스의 일치 여부를 확인하는 조인인 동등 조인을 수행합니다.
Join, GroupJoin
List<Student> students = new List<Student>
{
new Student{grade = 3, name = "Tom", scores = new int[] {60, 80} },
new Student{grade = 3, name = "Brian" , scores = new int[] {50, 70} },
new Student{grade = 4, name = "Chris" , scores = new int[] {80, 80}},
new Student{grade = 5, name = "James" , scores = new int[] {100, 100} },
};
List<Teacher> teachers = new List<Teacher>
{
new Teacher{grade = 3, name = "Miss Christina" },
new Teacher{grade = 4, name = "Mr. Tayler" },
new Teacher{grade = 6, name = "Mr. Jone" },
new Teacher{grade = 5, name = "James" },
};
Console.WriteLine("join by grade===");
var join = teachers.Join(students,
t => t.grade,
s => s.grade,
(t, s) => new { grade = t.grade, name = t.name, studentName = s.name });
var query = from teacher in teachers
join student in students on teacher.grade equals student.grade
select new { teacher.grade, teacher.name, studentName = student.name };
foreach (var teacher in query)
{
Console.WriteLine(teacher.grade + "," + teacher.name, 8);
}
/*
3, Miss Christina
3, Miss Christina
4, Mr. Tayler
*/
Console.WriteLine("join by name===");
var joinName = teachers.Join(students,
t => t.name,
s => s.name,
(t, s) => new { grade = t.grade, name = t.name, studentName = s.name });
foreach (var teacher in joinName)
{
Console.WriteLine(teacher.grade + ", " + teacher.name + ", " + teacher.studentName);
}
/*
5, James, James
*/
Console.WriteLine("group join===");
var grpJoin = from teacher in teachers
join student in students on teacher.grade equals student.grade into grp
select grp;
foreach (var key in grpJoin)
{
foreach (var teacher in key)
{
Console.WriteLine(teacher.grade + "," + teacher.name, 8);
}
}
/*
3, Tom
3, Brian
4, Chris
6, James
*/
GroupJoin 결과집합
- Key = 3 Tom, Brain
- Key = 4 Chris
- Key = 5 null
- Key = 6 James
그룹
시퀀스의 요소를 그룹화
GroupBy, Lookup
List<Student> students = new List<Student>
{
new Student{grade = 3, name = "Tom", scores = new int[] {60, 80} },
new Student{grade = 3, name = "Brian" , scores = new int[] {50, 70} },
new Student{grade = 4, name = "Chris" , scores = new int[] {80, 80}},
new Student{grade = 5, name = "James" , scores = new int[] {100, 100} },
};
var grpByQuery = from student in students
group student by student.grade;
var groupByMethod = students.GroupBy(student => student.grade);
foreach (var grp in groupByMethod)
{
Console.WriteLine("key : " + grp.Key);
foreach(var item in grp)
{
Console.WriteLine(" " + item.name);
}
}
/*
Key : 3
Tom
Brain
Key : 4
Chris
Key : 5
James
*/
// ToLookUp
// Key, Element 로 그룹화
var lookup = students.ToLookup(student => student.grade, student=> student.grade + ":" +student.name);
foreach (var grp in lookup)
{
Console.WriteLine("Lookup key : " + grp.Key);
foreach (var item in grp)
{
Console.WriteLine(" " + item);
}
}
/*
Lookup key : 3
3:Tom
3:Brain
Lookup key : 4
4:Chris
Lookup key : 5
5:James
*/
foreach (var grp in lookup)
{
Console.WriteLine("Lookup key : " + grp.Key);
foreach (var item in lookup[grp.Key])
{
Console.WriteLine(" lookup " + item);
}
}
/*
Lookup key : 3
lookup 3:Tom
lookup 3:Brain
Lookup key : 4
lookup 4:Chris
Lookup key : 5
lookup 5:James
*/
집계
Aggregate, Average, Count, Max, MIn, Sum
List<Student> students = new List<Student>
{
new Student{grade = 3, name = "Tom", scores = new int[] {60, 80} },
new Student{grade = 3, name = "Brian" , scores = new int[] {50, 70} },
new Student{grade = 4, name = "Chris" , scores = new int[] {80, 80}},
new Student{grade = 5, name = "James" , scores = new int[] {100, 100} },
};
Console.WriteLine(students.Average(student => student.scores.Average()));
/*
77.5 // 각 요소의 scores 의 평균 값의 평균 => (70 + 60 +80 +100) / 4
*/
Console.WriteLine(students.Count());
/*
4
*/
Console.WriteLine(students.Max(student=> student.scores.Max()));
/*
100 // 각 요소의 scores 의 최대값중 최대값(100)
*/
Console.WriteLine(students.Min(student => student.scores.Min()));
/*
50 // 각 요소의 scores 의 최소값중 최소값(50)
*/
Console.WriteLine(students.Sum(student => student.scores.Sum()));
/*
620 // 각 요소의 scores 의 합계의 총합(620)
*/
반응형