Quantifier query operators’ works on only single input sequence and return single Boolean value.
Contains
The Contains query operator evaluates the elements in the input sequence and returns true if a specified value exists.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQTutorial { class Program { static void Main(string[] args) { string[] items = { "item1", "item2", "item3" }; //return true var item = items.Contains("item2"); //return false var item1 = items.Contains("item4"); Console.WriteLine("Result is " + item +" Missing Item "+item1); Console.ReadLine(); } } }
Any
Any operator works in two ways or we can say that there are two overloaded version of any operator.
- Any return true if any element is exists in list.
- If predicate or lambda expression is used then any operator return true when element is matched in list which satisfied the specified predicate condition.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQTutorial { class Program { static void Main(string[] args) { string[] items = { }; var result = items.Any(); //It will return because items array does not have any element. Console.WriteLine("items in Items array are exists " + result); int[] numbers = { 2, 3, 4, 5, 9, 10 }; //Check element which are divisble by 3 completely. var numexists = numbers.Any(i => i % 3 == 0); Console.WriteLine("Elements are divisible by 3" + numexists); Console.ReadLine(); } } }
All
The All query operator takes a predicate and evaluates the elements in the input sequence to determine if every element satisfies this predicate.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQTutorial { class Program { static void Main(string[] args) { int[] numbers = { 2, 3, 4, 5, 9, 10 }; //This will return false because all elements in numbers are not completely divisble by 3 var numexists = numbers.All(i => i % 3 == 0); Console.WriteLine("Elements are divisible by 3" + numexists); Console.ReadLine(); } } }
SequenceEqual
The SequenceEqual query operator compares two sequences to see if they both have exactly the same elements in the same order.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQTutorial { class Program { static void Main(string[] args) { string[] items1 = {"item1","item2","item2" }; string[] items2 = { "item1", "item2", "item2" }; //Check element which are divisble by 3 completely. var sameitems = items1.SequenceEqual(items2); Console.WriteLine("Both List have same items "+sameitems); Console.ReadLine(); } } }