Hello Friends, In this tutorial we are going to understand what is Any Operator in Linq? Any Operator is a Qualifier Operators in Linq Query Operators. It returns a boolean value. It is used to check whether any elements in a sequence satisfy a condition or not.

What is Any Operator in Linq?

Any Operator is a Qualifier Operators in Linq Query Operators. It returns a boolean value. It is used to check whether any elements in a sequence satisfy a condition or not. It is also used to check whether a sequence is empty or not. Any Operator can only be used with method syntax, not with query syntax.

Example1:

In this example, you will see how Any Operator works.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using System.Runtime.Serialization.Json;
namespace AnyOperatorDemo
{


class Program
{
 
static void Main(string[] args)
{
var numbers = new[] { 1, 2, 3, 4, 5 };
var isNotEmpty = numbers.Any(); Console.WriteLine(isNotEmpty); //True
var anyNumberIsOne = numbers.Any(n => n == 1); Console.WriteLine(anyNumberIsOne); //True
var anyNumberIsSix = numbers.Any(n => n == 6); Console.WriteLine(anyNumberIsSix); //False 
var anyNumberIsOdd = numbers.Any(n => (n & 1) == 1); Console.WriteLine(anyNumberIsOdd); //True
var anyNumberIsNegative = numbers.Any(n => n < 0); Console.WriteLine(anyNumberIsNegative); //False
Console.ReadLine(); 
}
 
}
}

Example 2:

In this example we need to check any student is having marks less than 200 or not.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using System.Runtime.Serialization.Json;
namespace AnyOperatorDemo
{

public class Student 
{
public string RollNo { get; set; }
public string StudentName { get; set; } 
public double MO { get; set; }
public double MM { get; set; }
}
class Program
{
public static List<Student> DataSource()
{
List<Student> lstStudent = new List<Student>();
Student objStudent1 = new Student() { RollNo = "R001", StudentName = "Ayush Singh", MO = 400, MM =500 };
Student objStudent2 = new Student() { RollNo = "R002", StudentName = "Ritwik Nigam", MO = 455, MM = 500 };
Student objStudent3 = new Student() { RollNo = "R003", StudentName = "Nikhil Bakshi", MO = 432, MM = 500 };
Student objStudent4 = new Student() { RollNo = "R004", StudentName = "Nidhi Verma", MO = 466, MM = 500 };
Student objStudent5 = new Student() { RollNo = "R005", StudentName = "Ayushman Lakhotia", MO = 478, MM = 500 };
Student objStudent6 = new Student() { RollNo = "R006", StudentName = "Neha Sharma", MO = 400, MM = 500 };
lstStudent.Add(objStudent1);
lstStudent.Add(objStudent2);
lstStudent.Add(objStudent3);
lstStudent.Add(objStudent4);
lstStudent.Add(objStudent5);
lstStudent.Add(objStudent6);
return lstStudent;
}
 
static void Main(string[] args)
{
List<Student> lstStudent = DataSource();
if (lstStudent.Any(s => s.MO < 200))
Console.WriteLine("Students having marks less than 200 exists"); 
else
Console.WriteLine("No Student is having marks less than 200");
 
Console.ReadLine(); 
}
 
}
}

Output:

No Student is having marks less than 200

View More:

Conclusion:

I hope you would love this post. Please feel free to comment for any technical help. Your feedback and suggestions are valuable to me.

Thank You.

Leave a Comment