Free Web Development Online Tutorials, Learn to Code
Learn LINQ with C#, Language Integrated query with different Data Source

LINQ Any and All Example in C#

How to use Any All methods in LINQ example

LINQ All method in C#

The All() method (of System.LINQ.Queryable class) returns a boolean value, check if all the elements of any collection satisfy the given condition.

Here is an example of using All method in LINQ
In following example we find out if all place name in the array starts with letter C

string[] placeArray = { "Calcutta", "Canada", "Costarica", "California", "Cameron", "Coimbatore" };
bool IsFisrtLetterC = placeArray.AsQueryable().All(p => p.StartsWith("C"));
bool IsLastLetterA = placeArray.AsQueryable().All(p => p.EndsWith("a"));
Console.WriteLine(IsFisrtLetterC); // true
Console.WriteLine(IsLastLetterA); // False

LINQ Any Method C# Example

The Any method (of System.LINQ.Queryable class) returns a boolean value, check if any of the elements of any collection satisfy the given condition.

Now let’s look at any method example
From below student list we check if any student has scored equal or more than 10

var studentsStream = new List<Student> {
new Student { FullName = "Aruna", StreamId=1, Score = 10 },
new Student { FullName = "Janet", StreamId=2, Score = 9 },
new Student { FullName = "Ajay", StreamId=1, Score = 11 },
new Student { FullName = "Kunal", StreamId=2, Score = 13 },
new Student { FullName = "Chandra", StreamId=2, Score = 8 },
};
bool hasScore10 = studentsStream.AsQueryable().Any(s => s.Score >= 10);
Console.WriteLine(hasScore10); // True
Comment
Name
Email
Website
Subscribe
 
Learn LINQ .Net

LINQ Any method and LINQ ALL method C# example

linq Interview Questions Answers

LINQ C# examples | Join Asp.net MVC Course