عملگر تجمیع Max
عملگر Max در زبان کوئری نویسی LINQ به منظور پیدا کردن بزرگترین عنصر در یک مجموعه استفاده می شود. در مثال زیر با استفاده از این عملگر بزرگترین عدد و همچنین بزرگترین عدد زوج موجود در مجموعه intList را پیدا میکنیم:
1 2 3 4 5 6 7 8 9 10 | IList<int> intList = new List<int>() { 10, 21, 30, 45, 50, 87 }; var largest = intList.Max(); Console.WriteLine("Largest Element: {0}", largest); var largestEvenElements = intList.Max(i => { if(i%2 == 0) return i; return 0; }); Console.WriteLine("Largest Even Element: {0}", largestEvenElements ); |
خروجی مثال:
1 2 | Largest Element: 87 Largest Even Element: 50 |
مثال زیر نحوه استفاده از این عملگر بر روی یک مجموعه نسبتا پیچیدهتر را نشان می دهد:
1 2 3 4 5 6 7 8 9 | IList<Student> studentList = new List<Student>>() { new Student() { StudentID = 1, StudentName = "John", Age = 13} , new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } , new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } , new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} , new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } }; var oldest = studentList.Max(s => s.Age); Console.WriteLine("Oldest Student Age: {0}", oldest); |
خروجی مثال:
1 | Oldest Student Ag: 21 |
در مثال زیر با استفاده از عملگر Max دانش آموزشی که بزرگترین نام (از نظر طول) را دارد انتخاب میکنیم:
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 27 28 29 30 31 | public class Student : IComparable<Student> { public int StudentID { get; set; } public string StudentName { get; set; } public int Age { get; set; } public int StandardID { get; set; } public int CompareTo(Student other) { if (this.StudentName.Length >= other.StudentName.Length) return 1; return 0; } } class Program { static void Main(string[] args) { // Student collection IList<Student> studentList = new List<Student>>() { new Student() { StudentID = 1, StudentName = "John", Age = 13} , new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } , new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } , new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} , new Student() { StudentID = 5, StudentName = "Steve" , Age = 15 } }; var studentWithLongName = studentList.Max(); Console.WriteLine("Student ID: {0}, Student Name: {1}", .StudentID, studentWithLongName.StudentName) } } |
خروجی مثال:
1 | Student ID: 5, Student Name: Steve |
توجه داشته باشید که عملگر Max در سینتکس کوئری و زبان سی شارپ پشتیبانی نمی شود. اما زبان VB.NET همانطور که در مثال زیر مشاهده می کنید، از آن پشتیبانی می کند:
1 2 3 4 5 6 7 8 9 | Dim studentList = New List(Of Student) From { New Student() With {.StudentID = 1, .StudentName = "John", .Age = 13}, New Student() With {.StudentID = 2, .StudentName = "Moin", .Age = 21}, New Student() With {.StudentID = 3, .StudentName = "Bill", .Age = 18}, New Student() With {.StudentID = 4, .StudentName = "Ram", .Age = 20}, New Student() With {.StudentID = 5, .StudentName = "Ron", .Age = 15} } Dim maxAge = Aggregate st In studentList Into Max(st.Age) Console.WriteLine("Maximum Age of Student: {0}", maxAge); |
خروجی مثال:
1 | Maximum Age of Student: 21 |
هیچ نظری ثبت نشده است