شبکه ایمن

مثال هایی از LINQ – آموزش LINQ

مثال هایی از LINQ

در این بخش از دو مجموعه Student و Standard که در زیر مشاهده می کنید، برای نوشتن کوئری LINQ استفاده خواهیم کرد:

IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 18, StandardID = 1 } , new Student() { StudentID = 2, StudentName = "Steve", 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 = 21 } }; 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"} };

123456789101112 IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 18, StandardID = 1 } , new Student() { StudentID = 2, StudentName = "Steve", 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 = 21 } };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"}};

Select چندگانه و عملگر Where

var studentNames = studentList.Where(s => s.Age > 18) .Select(s => s) .Where(st => st.StandardID > 0) .Select(s => s.StudentName);

1234 var studentNames = studentList.Where(s => s.Age > 18) .Select(s => s) .Where(st => st.StandardID > 0) .Select(s => s.StudentName);

خروجی:

Steve Ram

12 SteveRam

در مثال زیر کوئری ما یک Enumerable از شیء anonymous باز میگرداند که فقط شامل خاصیت StudentName است:

var teenStudentsName = from s in studentList where s.age > 12 && s.age < 20 select new { StudentName = s.StudentName }; teenStudentsName.ToList().ForEach(s => Console.WriteLine(s.StudentName));

1234 var teenStudentsName = from s in studentList where s.age > 12 && s.age < 20 select new { StudentName = s.StudentName };teenStudentsName.ToList().ForEach(s => Console.WriteLine(s.StudentName));

خروجی:

John Bill

12 JohnBill

گروه بندی

در مثال زیر لیست دانش آموزان را توسط خاصیت StandardID گروه بندی می کنیم:

var studentsGroupByStandard = from s in studentList group s by s.StandardID into sg orderby sg.Key select new { sg.Key, sg }; foreach (var group in studentsGroupByStandard) { Console.WriteLine("StandardID {0}:", group.Key); group.sg.ToList().ForEach(st => Console.WriteLine(st.StudentName )); }

12345678910 var studentsGroupByStandard = from s in studentList group s by s.StandardID into sg orderby sg.Key select new { sg.Key, sg };foreach (var group in studentsGroupByStandard){ Console.WriteLine("StandardID {0}:", group.Key); group.sg.ToList().ForEach(st => Console.WriteLine(st.StudentName ));}

خروجی:

StandardID 0: Ron StandardID 1: John Steve StandardID 2: Bill Ram

12345678 StandardID 0:RonStandardID 1:JohnSteveStandardID 2:BillRam

اگر به لیست تعریف شده در بالا توجه کنید متوجه می شوید که Ron هیچ StandardID ندارد به خاطر همین در گروه StandardID 0 قرار گرفته است (چون مقدار پیشفرض int صفر است). برای رفع این مورد می توانید از عملگر Where مانند نمونه زیر استفاده کنید:

var studentsGroupByStandard = from s in studentList where s.StandardID > 0 group s by s.StandardID into sg orderby sg.Key select new { sg.Key, sg };

12345 var studentsGroupByStandard = from s in studentList where s.StandardID > 0 group s by s.StandardID into sg orderby sg.Key select new { sg.Key, sg };

خروجی:

StandardID 1: John Steve StandardID 2: Bill Ram

123456 StandardID 1:JohnSteveStandardID 2:BillRam

Left Outer Join در LINQ

با استفاده از Left Outer Join می توانیم دانش آموزان را در هر استاندارد قرار دارند را نمایش دهیم:

var studentsGroup = from stad in standardList join s in studentList on stad.StandardID equals s.StandardID into sg select new { StandardName = stad.StandardName, Students = sg }; foreach (var group in studentsGroup) { Console.WriteLine(group.StandardName); group.Students.ToList().ForEach(st => Console.WriteLine(st.StudentName)); }

12345678910111213 var studentsGroup = from stad in standardList join s in studentList on stad.StandardID equals s.StandardID into sg select new { StandardName = stad.StandardName, Students = sg };foreach (var group in studentsGroup){ Console.WriteLine(group.StandardName); group.Students.ToList().ForEach(st => Console.WriteLine(st.StudentName));}

خروجی:

Standard 1: John Steve Standard 2: Bill Ram Standard 3:

1234567 Standard 1:JohnSteveStandard 2:BillRamStandard 3:

مرتب‌سازی

در نمونه زیر لیست دانش آموزان به صورت صعودی و توسط خاصیت StandardID و Age مرتب می شود:

var sortedStudents = from s in studentList orderby s.StandardID, s.age select new { StudentName = s.StudentName, Age = s.age, StandardID = s.StandardID }; sortedStudents.ToList().ForEach(s => Console.WriteLine("Student Name: {0}, Age: {1}, StandardID: {2}", s.StudentName, s.Age , s.StandardID));

1234567 var sortedStudents = from s in studentList orderby s.StandardID, s.age select new { StudentName = s.StudentName, Age = s.age, StandardID = s.StandardID };sortedStudents.ToList().ForEach(s => Console.WriteLine("Student Name: {0}, Age: {1}, StandardID: {2}", s.StudentName, s.Age , s.StandardID));

خروجی:

Student Name: Ron, Age: 21, StandardID: 0 Student Name: John, Age: 18, StandardID: 1 Student Name: Steve, Age: 21, StandardID: 1 Student Name: Bill, Age: 18, StandardID: 2 Student Name: Ram, Age: 20, StandardID: 2

12345 Student Name: Ron, Age: 21, StandardID: 0Student Name: John, Age: 18, StandardID: 1Student Name: Steve, Age: 21, StandardID: 1Student Name: Bill, Age: 18, StandardID: 2Student Name: Ram, Age: 20, StandardID: 2

Inner Join در LINQ

var studentWithStandard = from s in studentList join stad in standardList on s.StandardID equals stad.StandardID select new { StudentName = s.StudentName, StandardName = stad.StandardName }; studentWithStandard.ToList().ForEach(s => Console.WriteLine("{0} is in {1}", s.StudentName, s.StandardName ));

12345678 var studentWithStandard = from s in studentList join stad in standardList on s.StandardID equals stad.StandardID select new { StudentName = s.StudentName, StandardName = stad.StandardName };studentWithStandard.ToList().ForEach(s => Console.WriteLine("{0} is in {1}", s.StudentName, s.StandardName ));

خروجی:

John is in Standard 1 Steve is in Standard 1 Bill is in Standard 2 Ram is in Standard 2

1234 John is in Standard 1Steve is in Standard 1Bill is in Standard 2Ram is in Standard 2

کوئری تو در تو (Nested Query)

var nestedQueries = from s in studentList where s.age > 18 && s.StandardID == (from std in standardList where std.StandardName == "Standard 1" select std.StandardID).FirstOrDefault() select s; nestedQueries.ToList().ForEach(s => Console.WriteLine(s.StudentName));

1234567 var nestedQueries = from s in studentList where s.age > 18 && s.StandardID == (from std in standardList where std.StandardName == "Standard 1" select std.StandardID).FirstOrDefault() select s;nestedQueries.ToList().ForEach(s => Console.WriteLine(s.StudentName));

خروجی:

Steve

1 Steve

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد.