Most often, we do with List and find one or more items in list. Imagine that we want to look for a product in List using PrimaryKey. The code is:
foreach (Product p in this.Products)
{
if (p.PrimaryKey == primaryKey)
return p;
}
We also have another way that makes it better in performance as well as readability.
Predicate condition = delegate(Product p)
{
return p.PrimaryKey.Equals(primaryKey);
};
return this.Products.Find(condition);
To find all items: this.Products.FindAll(condition);
Both of them return NULL if not found.
Loading
Join the conversation! Your thoughts help the community grow.