Exist function in List Collection

Rajesh_Kumar
Posted by Rajesh_Kumar under LINQ category on | Points: 40 | Views : 671
Exist function returns true if item is present in List Collection otherwise returns false.

List<int> lst_numbers = new List<int>();
lst_numbers.Add(1);
lst_numbers.Add(2);
lst_numbers.Add(5);
lst_numbers.Add(7);
lst_numbers.Add(8);

//Check whether item exists in a list greater than 2
bool is_item_exists = lst_numbers.Exists(number => number > 2);
Response.Write(is_item_exists);

//We can also Check for numbers less than 8
is_item_exists = lst_numbers.Exists(number => number < 8);
Response.Write(is_item_exists);

//We can also Check for numbers less than 1
is_item_exists = lst_numbers.Exists(number => number < 1);
Response.Write(is_item_exists);


Output would be:
True
True
False

Comments or Responses

Login to post response