앞 부분에 이어서 계속 그룹화 시키는 방법을 알아 보도록 하겠습니다. StudentClass 클래스와 데이타를 모두 같이 사용합니다.
다음 예제에서는 숫자 범위를 그룹 키로 사용하여 그룹화하는 방법을 보여 줍니다. 쿼리는 FirstName, LastName 과 학생이 속한 백분위수 범위만 포함한 Anonymous type(익명 타입)으로 결과를 반환합니다.
결과를 표시하기 위해 완전한 Student 개체를 사용할 필요가 없으므로 익명 형식이 사용됩니다.
GetPercentile은 학생의 평균 점수에 기초하여 백분위수를 계산하는 함수입니다. 이 메서드는 0에서 10 사이의 정수를 반환합니다.
protected static int GetPercentile(Student s) { double avg = s.ExamScores.Average(); return avg > 0 ? (int)avg / 10 : 0; }
참고로 List<T> 형식의 데이타에서 평균값을 구하는 method 가 Average() 입니다. 최고값은 Max(), 최저값은 Min(), 아이템 개수는 Count() 입니다.
어째든, 다음 함수가 실제적으로 그룹화를 시키는 내용입니다.
public void GroupByRange() { Console.WriteLine("\r\n숫자 범위로 그룹화 시키기:"); var queryNumericRange = from student in students let percentile = GetPercentile(student) group new { student.FirstName, student.LastName } by percentile into percentGroup orderby percentGroup.Key select percentGroup; // Nested foreach required to iterate over groups and group items. foreach (var studentGroup in queryNumericRange) { Console.WriteLine("Key: {0}", (studentGroup.Key * 10)); foreach (var item in studentGroup) { Console.WriteLine("\t{0}, {1}", item.LastName, item.FirstName); } } }
전체 소스
public class StudentClass { #region data protected enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear }; protected class Student { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public GradeLevel Year; public List<int> ExamScores; } protected static List<string> students = new List<string> { new Student {FirstName = "Terry", LastName = "Adams", ID = 120, Year = GradeLevel.SecondYear, ExamScores = new List<int>{ 99, 82, 81, 79}}, new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116, Year = GradeLevel.ThirdYear,ExamScores = new List<int>{ 99, 86, 90, 94}}, new Student {FirstName = "Hanying", LastName = "Feng", ID = 117, Year = GradeLevel.FirstYear, ExamScores = new List<int>{ 93, 92, 80, 87}}, new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114, Year = GradeLevel.FourthYear,ExamScores = new List<int>{ 97, 89, 85, 82}}, new Student {FirstName = "Debra", LastName = "Garcia", ID = 115, Year = GradeLevel.ThirdYear, ExamScores = new List<int>{ 35, 72, 91, 70}}, new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118, Year = GradeLevel.SecondYear, ExamScores = new List<int>{ 92, 90, 83, 78}}, new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113, Year = GradeLevel.FirstYear, ExamScores = new List<int>{ 88, 94, 65, 91}}, new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112, Year = GradeLevel.FourthYear, ExamScores = new List<int>{ 75, 84, 91, 39}}, new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111, Year = GradeLevel.SecondYear, ExamScores = new List<int>{ 97, 92, 81, 60}}, new Student {FirstName = "Lance", LastName = "Tucker", ID = 119, Year = GradeLevel.ThirdYear, ExamScores = new List<int>{ 68, 79, 88, 92}}, new Student {FirstName = "Michael", LastName = "Tucker", ID = 122, Year = GradeLevel.FirstYear, ExamScores = new List<int>{ 94, 92, 91, 91}}, new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121, Year = GradeLevel.FourthYear, ExamScores = new List<int>{ 96, 85, 91, 60}} }; #endregion //Helper method, used in GroupByRange. protected static int GetPercentile(Student s) { double avg = s.ExamScores.Average(); return avg > 0 ? (int)avg / 10 : 0; } #region GroupByRange public void GroupByRange() { Console.WriteLine("\r\n숫자 범위로 그룹화 시키기:"); var queryNumericRange = from student in students let percentile = GetPercentile(student) group new { student.FirstName, student.LastName } by percentile into percentGroup orderby percentGroup.Key select percentGroup; // Nested foreach required to iterate over groups and group items. foreach (var studentGroup in queryNumericRange) { Console.WriteLine("Key: {0}", (studentGroup.Key * 10)); foreach (var item in studentGroup) { Console.WriteLine("\t{0}, {1}", item.LastName, item.FirstName); } } } #endregion } class Program { static void Main(string[] args) { StudentClass sc = new StudentClass(); sc.GroupByRange(); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit"); Console.ReadKey(); } }
다음 예제에서는 부울 비교 식을 사용하여 소스 요소를 그룹화하는 방법을 보여 줍니다.
이 예제에서 부울 식은 학생의 평균 시험 점수가 75점을 넘는지 확인합니다. 위의 예제와 같이 완전한 소스 요소가 필요하지 않으므로 결과는 익명 형식으로 반환됩니다. 익명 형식의 속성은 Key 멤버의 속성이 되고 쿼리 실행 시에 이름으로 액세스할 수 있습니다.
결론은 평균 점수가 75점을 넘는지,못 넘는지를 넘는지를 기준으로 두 그룹으로 나뉘는 쿼리문입니다. 비교의 기준은 by 절에 있는 by student.ExamScores.Average() > 75 절입니다.
public void GroupByBoolean() { Console.WriteLine("\r\nGroup by a Boolean into two groups with string keys"); Console.WriteLine("\"True\" and \"False\" and project into a new anonymous type:"); var queryGroupByAverages = from student in students group new { student.FirstName, student.LastName } by student.ExamScores.Average() > 75 into studentGroup select studentGroup; foreach (var studentGroup in queryGroupByAverages) { Console.WriteLine("Key: {0}", studentGroup.Key); foreach (var student in studentGroup) Console.WriteLine("\t{0} {1}", student.FirstName, student.LastName); } }
전체 소스
public class StudentClass { #region data protected enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear }; protected class Student { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public GradeLevel Year; public List<int> ExamScores; } protected static List<Student> students = new List<Student> { new Student {FirstName = "Terry", LastName = "Adams", ID = 120, Year = GradeLevel.SecondYear, ExamScores = new List<int>{ 99, 82, 81, 79}}, new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116, Year = GradeLevel.ThirdYear,ExamScores = new List<int>{ 99, 86, 90, 94}}, new Student {FirstName = "Hanying", LastName = "Feng", ID = 117, Year = GradeLevel.FirstYear, ExamScores = new List<int>{ 93, 92, 80, 87}}, new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114, Year = GradeLevel.FourthYear,ExamScores = new List<int>{ 97, 89, 85, 82}}, new Student {FirstName = "Debra", LastName = "Garcia", ID = 115, Year = GradeLevel.ThirdYear, ExamScores = new List<int>{ 35, 72, 91, 70}}, new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118, Year = GradeLevel.SecondYear, ExamScores = new List<int>{ 92, 90, 83, 78}}, new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113, Year = GradeLevel.FirstYear, ExamScores = new List<int>{ 88, 94, 65, 91}}, new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112, Year = GradeLevel.FourthYear, ExamScores = new List<int>{ 75, 84, 91, 39}}, new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111, Year = GradeLevel.SecondYear, ExamScores = new List<int>{ 97, 92, 81, 60}}, new Student {FirstName = "Lance", LastName = "Tucker", ID = 119, Year = GradeLevel.ThirdYear, ExamScores = new List<int>{ 68, 79, 88, 92}}, new Student {FirstName = "Michael", LastName = "Tucker", ID = 122, Year = GradeLevel.FirstYear, ExamScores = new List<int>{ 94, 92, 91, 91}}, new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121, Year = GradeLevel.FourthYear, ExamScores = new List<int>{ 96, 85, 91, 60}} }; #endregion //Helper method, used in GroupByRange. protected static int GetPercentile(Student s) { double avg = s.ExamScores.Average(); return avg > 0 ? (int)avg / 10 : 0; } #region GroupByBoolean public void GroupByBoolean() { Console.WriteLine("\r\nGroup by a Boolean into two groups with string keys"); Console.WriteLine("\"True\" and \"False\" and project into a new anonymous type:\n"); var queryGroupByAverages = from student in students group new { student.FirstName, student.LastName } by student.ExamScores.Average() > 75 into studentGroup select studentGroup; foreach (var studentGroup in queryGroupByAverages) { Console.WriteLine("Key: {0}", studentGroup.Key); foreach (var student in studentGroup) Console.WriteLine("\t{0} {1}", student.FirstName, student.LastName); } } #endregion } class Program { static void Main(string[] args) { StudentClass sc = new StudentClass(); sc.GroupByBoolean(); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit"); Console.ReadKey(); } }
'C# with LINQ to SQL' 카테고리의 다른 글
LINQ - 4칙 연산(select, insert, update, delete) - DataGridView 이용 (0) | 2015.07.01 |
---|---|
LINQ - 로그인창 적용 (0) | 2015.06.25 |
LINQ - 조인(내부조인, 그룹조인, 왼쪽 우선 외부 조인) (0) | 2015.06.22 |
LINQ - 그룹핑1 (0) | 2015.06.18 |
LINQ - 소개 (0) | 2015.06.16 |