عملگر پیوند GroupJoin
در بخش قبلی با عملگر Join آشنا شدیم. در این بخش عملگر GroupJoin در زبان کوئری نویسی LINQ را بررسی خواهیم کرد. این عملگر همان کار عملگر Join را انجام می دهد با این تفاوت که نتیجه بازگشتی به صورت گروهی از مجموعه ها است. این گروه بندی بر اساس کلیدی انجام می شود که به منظور پیوند دادن دو مجموعه مشخص شده است. ورودی های متد GroupJoin با متد Join یکسان است و همانطور که گفتیم فقط در خروجی تفاوت دارند. در زیر می توانید overload های مربوط به این متد را مشاهده کنید:
1 2 3 | public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector); public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer); |
حال برای درک بهتر مثال بخش قبل را این بار با استفاده از متد GroupJoin پیادهسازی می کنیم. کلاس های مورد نیاز:
1 2 3 4 5 6 7 8 9 | public class Student{ public int StudentID { get; set; } public string StudentName { get; set; } public int StandardID { get; set; } } public class Standard{ public int StandardID { get; set; } public string StandardName { get; set; } } |
نحوه استفاده از متد GroupJoin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", StandardID =1 }, new Student() { StudentID = 2, StudentName = "Moin", StandardID =1 }, new Student() { StudentID = 3, StudentName = "Bill", StandardID =2 }, new Student() { StudentID = 4, StudentName = "Ram", StandardID =2 }, new Student() { StudentID = 5, StudentName = "Ron" } }; IList<Standard> standardList = new List<Standard>() { new Standard(){ StandardID = 1, StandardName="Standard 1"}, new Standard(){ StandardID = 2, StandardName="Standard 2"}, new Standard(){ StandardID = 3, StandardName="Standard 3"} }; var groupJoin = standardList.GroupJoin(studentList, //inner sequence std => std.StandardID, //outerKeySelector s => s.StandardID, //innerKeySelector (std, studentsGroup) => new // resultSelector { Students = studentsGroup, StandarFulldName = std.StandardName }); foreach (var item in groupJoin) { Console.WriteLine(item.StandarFulldName ); foreach(var stud in item.Students) Console.WriteLine(stud.StudentName); } |
خروجی مثال به شکل زیر خواهد بود:
1 2 3 4 5 6 7 | Standard 1: John, Moin, Standard 2: Bill, Ram, Standard 3: |
متد GroupJoin در سینتکس کوئر
نحوه استفاده از عملگر GroupJoin در سینتکس کوئری کمی با سینتکس متد فرق دارد. در سینتکس کوئری باید مجموعه بیرونی، انتخاب کننده کلید و انتخاب کننده نتیجه مشخص شوند. کلمه کلیدی ‘on’ برای مشخص کردن کلید ها استفاده می شود که در آن مقداری که قبل از equals قرار دارد، کلید بیرونی و مقداری که بعد equals قرار دارد، کلید درونی است. همچنین با استفاده از کلمه کلیدی into می توانیم مجموعه گروه بندی شده ایجاد کنیم. سینتکس کلی به شکل زیر است:
1 2 3 4 5 | from ... in outerSequence join ... in innerSequence on outerKey equals innerKey into groupedCollection select ... |
در مثال زیر ما همان کاری که در نمونه فوق انجام دادیم را با استفاده از سینتکس کوئری پیادهسازی میکنیم:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 13, StandardID =1 }, new Student() { StudentID = 2, StudentName = "Moin", Age = 21, StandardID =1 }, new Student() { StudentID = 3, StudentName = "Bill", Age = 18, StandardID =2 }, new Student() { StudentID = 4, StudentName = "Ram" , Age = 20, StandardID =2 }, new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } }; IList<Standard> standardList = new List<Standard>() { new Standard(){ StandardID = 1, StandardName="Standard 1"}, new Standard(){ StandardID = 2, StandardName="Standard 2"}, new Standard(){ StandardID = 3, StandardName="Standard 3"} }; var groupJoin = from std in standardList join s in studentList on std.StandardID equals s.StandardID into studentGroup select new { Students = studentGroup , StandardName = std.StandardName }; foreach (var item in groupJoin) { Console.WriteLine(item.StandarFulldName ); foreach(var stud in item.Students) Console.WriteLine(stud.StudentName); } |
خروجی مثال:
1 2 3 4 5 6 7 | Standard 1: John, Moin, Standard 2: Bill, Ram, Standard 3: |
هیچ نظری ثبت نشده است