LINQ All Method
LINQ All Method
دالة All()
تُستخدم للتحقق إذا كانت جميع العناصر في المجموعة تحقق شرطًا معينًا.
- ترجع
true
لو كل العناصر طابقت الشرط. - ترجع
false
لو عنصر واحد فقط فشل في الشرط.
✅ مثال 1: مع أرقام
int[] numbers = { 10, 20, 30, 40 };
bool result = numbers.All(n => n > 5);
Console.WriteLine(result); // True
bool result = numbers.All(n => n > 25);
Console.WriteLine(result); // False
✅ مثال 2: مع النصوص
string[] names = { "Ahmed", "Ali", "Amal", "Alaa" };
bool result = names.All(name => name.StartsWith("A"));
Console.WriteLine(result); // True
string[] names = { "Ahmed", "Ali", "Sara", "Alaa" };
bool result = names.All(name => name.StartsWith("A"));
Console.WriteLine(result); // False
🧩 مثال 3: مع الكائنات
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
}
List<Student> studentList = new List<Student>()
{
new Student() { ID = 1, Name = "Ahmed", Marks = 80 },
new Student() { ID = 2, Name = "Sara", Marks = 95 },
new Student() { ID = 3, Name = "Mahmoud", Marks = 70 }
};
bool allPassed = studentList.All(s => s.Marks >= 50);
Console.WriteLine("All Passed: " + allPassed); // True
💡 ملاحظات هامة:
- لو المجموعة فاضية →
All()
بترجعtrue
لأن مفيش عنصر فشل في الشرط. - مفيدة جدًا في التحقق الجماعي: هل كل الطلبة نجحوا؟ هل كل العناصر تبدأ بحرف معين؟
تعليقات
إرسال تعليق