Thursday, September 15, 2011

Linq : Check Value Exists in Genric List of Object

Method 1:
Use Any

bool exists = Users.Any(usr => usr.id == "112");

Method 2:
Using Contains

List<string> FruitNames = new List { "Apple", "Banana", "Orange" };
bool exists = Fruits.Any(fruit => fruit.Contains(“Orange”));

you can also fine the specific object using LINQ find statement    

Person person = personList.Find(delegate(Person pers) {return pers.ID == 1; });

No comments :

Post a Comment