Back to: LINQ Tutorial For Beginners and Professionals
LINQ All Method in C# with Examples
In this article, I am going to discuss the LINQ All Method in C# with Examples. Please read our previous article where we discussed the basics of LINQ Quantifiers Methods in C#. As part of this article, we are going to discuss the following pointers which are related to LINQ All Operator.
- What is Linq All Operator?
- Multiple Examples to Understand LINQ ALL Method using both Method and Query Syntax.
What is LINQ All Method in C#?
The LINQ All Method in C# is used to check whether all the elements of a data source satisfy a given condition or not. If all the elements satisfy the given condition, then it returns true else returns false. There is no overloaded version is available for the LINQ All Method. The definition of the LINQ ALL Method is given below. As you can see in the below image, the ALL Extension method takes one predicate as a parameter.
Example to Understand LINQ All Method in C# using Value Type
Let us see an example to Understand LINQ All Method in C# using both Method and Query Syntax. The following example returns true as all the elements are greater than 10 in the integer array. There is no operator called all in Query Syntax, so we need to use Mixed Syntax.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { int[] IntArray = { 11, 22, 33, 44, 55 }; //Using Method Syntax bool ResultMS = IntArray.All(x => x > 10); //Using Query Syntax bool ResultQS = (from num in IntArray select num).All(x => x > 10); Console.WriteLine("Is All Numbers are greater than 10 : " + ResultMS); Console.ReadKey(); } } }
Output: Is All Numbers are greater than 10 : True
Example to Understand LINQ All Method in C# using String Type
Let us see an example to Understand How to use LINQ All Method in C# using String type collection. For a better understanding, please have a look at the following example which shows how to use All Method in C# with String type collection. The following example will return false as all the names are not greater than 5 characters.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { string[] stringArray = { "James", "Sachin", "Sourav", "Pam", "Sara" }; //Using Method Syntax bool ResultMS = stringArray.All(name => name.Length > 5); //Using Query Syntax bool ResultQS = (from num in stringArray select num).All(name => name.Length > 5); Console.WriteLine("Is All Names are greater than 5 Characters : " + ResultQS); Console.ReadKey(); } } }
Output: Is All Names are greater than 5 Characters : False
Example to Understand LINQ All Method with Complex Type in C#:
Let us see an example to Understand How to use LINQ All Method with Complex Data Type in C# using both Method and Query Syntax. We are going to work with the following Student and Subject classes. So, create a class file with the name Student.cs and then copy and paste the following code. As you can see, the Student class has four properties such as ID, Name, TotalMarks, and Subjects. Here, within the Student class, we have also created one method i.e. GetAllStudnets() which will return the list of all the students. The Subject class has only two properties i.e. SubjectName and Marks.
using System.Collections.Generic; namespace LINQDemo { public class Student { public int ID { get; set; } public string Name { get; set; } public int TotalMarks { get; set; } public List<Subject> Subjects { get; set; } public static List<Student> GetAllStudnets() { List<Student> listStudents = new List<Student>() { new Student{ID= 101,Name = "Preety", TotalMarks = 265, Subjects = new List<Subject>() { new Subject(){SubjectName = "Math", Marks = 80}, new Subject(){SubjectName = "Science", Marks = 90}, new Subject(){SubjectName = "English", Marks = 95} }}, new Student{ID= 102,Name = "Priyanka", TotalMarks = 278, Subjects = new List<Subject>() { new Subject(){SubjectName = "Math", Marks = 90}, new Subject(){SubjectName = "Science", Marks = 95}, new Subject(){SubjectName = "English", Marks = 93} }}, new Student{ID= 103,Name = "James", TotalMarks = 240, Subjects = new List<Subject>() { new Subject(){SubjectName = "Math", Marks = 70}, new Subject(){SubjectName = "Science", Marks = 80}, new Subject(){SubjectName = "English", Marks = 90} }}, new Student{ID= 104,Name = "Hina", TotalMarks = 275, Subjects = new List<Subject>() { new Subject(){SubjectName = "Math", Marks = 90}, new Subject(){SubjectName = "Science", Marks = 90}, new Subject(){SubjectName = "English", Marks = 95} }}, new Student{ID= 105,Name = "Anurag", TotalMarks = 255, Subjects = new List<Subject>() { new Subject(){SubjectName = "Math", Marks = 80}, new Subject(){SubjectName = "Science", Marks = 90}, new Subject(){SubjectName = "English", Marks = 85} } }, }; return listStudents; } } public class Subject { public string SubjectName { get; set; } public int Marks { get; set; } } }
Now, our requirement is to check whether all the students have total marks greater than 250 or not. As you can see the student James total mark is 240 which is less than 250. So here, the LINQ ALL method will give you the output as false. This is because the All method will return true when all the elements present in the collection satisfy the given condition.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { //Using Method Syntax bool MSResult = Student.GetAllStudnets().All(std => std.TotalMarks > 250); //Using Query Syntax bool QSResult = (from std in Student.GetAllStudnets() select std).All(std => std.TotalMarks > 250); Console.WriteLine($"Is All Students Having Total Marks 250: {MSResult}"); Console.ReadKey(); } } }
Output: Is All Students Having Total Marks 250: False
Complex Example to Understand LINQ All Method in C#:
Let us see a more Complex Example to Understand the LINQ All Method in C#. If you see our student’s collection, then you will observe that each student object has another collection called Subjects. Now we need to fetch all the student details whose mark on each subject is greater than 80. That means, now we will not apply the LINQ All method to the student’s collection, rather we will apply the LINQ All method to the Subject collection of each student. For a better understanding, please have a look at the following example. As we know, the Where Extension method takes a predicate as a parameter which will return a boolean true and false. Boolean TRUE means that the element will return and False means that the record will not return. Here, within the Where Extension method, on the Subject property, we are applying the LINQ All method. Now, for each student, the LINQ All method will execute and it will check whether all the Subject Marks satisfied the given condition i.e. Marks > 80, and if satisfied, the All Method will return True, and Where extension method will return that Student in output.
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { //Using Method Syntax var MSResult = Student.GetAllStudnets() .Where(std => std.Subjects.All(x => x.Marks > 80)).ToList(); //Using Query Syntax var QSResult = (from std in Student.GetAllStudnets() where std.Subjects.All(x => x.Marks > 80) select std).ToList(); foreach (var student in QSResult) { Console.WriteLine($"{student.Name} - {student.TotalMarks}"); foreach (var subject in student.Subjects) { Console.WriteLine($" {subject.SubjectName} - {subject.Marks}"); } } Console.ReadKey(); } } }
Output:
In the next article, I am going to discuss the LINQ Any Method in C# with Examples. In this article, I try to explain the Linq ALL Method in C# with Examples. I hope you understood the need and use of the LINQ All Method in C# with Examples.
This line at Example2:
The following example returns true as all the names are not greater than 5 characters.
Should be :
The following example returns FALSE as all the names are not greater than 5 characters.