Find in BindingList <T>
How to find an object in a BindingList that has a property equal to a specific value. Below is my code.
public class Product { public int ProductID { get; set; } public string ProductName { get; set; } } BindingList<Product> productList = new BindingList<Product>(); Now consider that productList has 100 products, and I want to find the product object with id 10.
I can find him using
productList.ToList<Product>().Find(p =>p.ProductID == 1); but I feel that using ToList () is undesirable here. Is there any direct way to do this, there is no Find method in the BindingList<T>
+4
1 answer
You can use SingleOrDefault from LINQ instead of Find :
Product product = productList.SingleOrDefault(p => p.ProductID == 1); product will be null if there were no such products. If there is more than one match, an exception will be thrown.
You really should take a look at LINQ to Objects - it greatly simplifies many data operations.
+8