Home > C# > List.Find() and Predicates

List.Find() and Predicates

List.Find() is the powerful method used to filter the values from the tones of  list items. It  reduce the line of code and improve the performance while filtering or searching the items.  You can specify a lambda expression as the argument to a List method that receives a Predicate type.

Syntax

public delegate bool Predicate<in T>(T obj)

Find() method takes the input as Perdicate delegate and return the boolean value.

Example:

 public class ProductInfo
{
public int ProductNumber { get; set; }
public string ProductName { get; set; }
public string ProductDesctiption { get; set; }
public string ProductBatchNumber { get; set; }
}

 public class ProductList
{
//Holds list of product Information
List<ProductInfo> objInfo = new List<ProductInfo>();

public void getProducts(string productname)
{
// Return the matching Value
ProductInfo prodInfo = objInfo.Find(delegate(ProductInfo info) { return info.ProductName == productname; });

//Other way
ProductInfo prodInfoLamdaParameter = objInfo.Find(O => O.ProductName == productname);
}

}

Advertisements
Categories: C#
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: