Professional Documents
Culture Documents
Practical Usage of Find Generic List
Practical Usage of Find Generic List
Practical Usage of Find Generic List
In this article we are going to see the Practical usage of Find<T> Method in Generic List<T> using Predicate.
Step 1:
Used Namespaces:
Step 2:
// <summary> /// This is static collection used in this tutorials /// </summary> /// <returns></returns> private List<CompanyProduct> AddCollection() { List<CompanyProduct> list = new List<CompanyProduct>(); list.Add(new CompanyProduct("MS Word", 1, "Microsoft", new DateTime(2012, 10, 31))); list.Add(new CompanyProduct("MS Excel", 2, "Microsoft", new DateTime(2011, 10, 31))); list.Add(new CompanyProduct("MS Powerpoint", 3, "Microsoft", new DateTime(2010, 10, 31))); list.Add(new CompanyProduct("Visual Studio", 2, "Microsoft", new DateTime(2011, 10, 31))); list.Add(new CompanyProduct("Sql Server", 3, "Microsoft", new DateTime(2010, 10, 31))); list.Add(new CompanyProduct("Oracle", 4, "Oracle", new DateTime(2011, 10, 31))); list.Add(new CompanyProduct("Sharepoint", 6, "Microsoft", new DateTime(2012, 10, 31))); list.Add(new CompanyProduct("MS Outlook", 7, "Microsoft", new DateTime(2011, 10, 31))); return list; }
Step 3:
Filtering the first product by company name using Predicate //Filter the first product by company name. CompanyProduct cp = list.Find(delegate(CompanyProduct item) { return item.CompanyName == "Microsoft"; });
Step 4:
//Filter the first product by product id. cp = list.Find(delegate(CompanyProduct item) { return item.ProductID == 1; }); Response.Write("<br/>"); Response.Write(string.Format(" Product={0} >>> CompanyName={1}", cp.Product, cp.CompanyName));
Step 5:
//Filter the first product by productRegisterDate. cp = list.Find(delegate(CompanyProduct item) { return item.ProductRegisterDate == new DateTime(2010, 10, 31); }); Response.Write("<br/>"); Response.Write(string.Format(" Product={0} >>> CompanyName={1}", cp.Product, cp.CompanyName));
Step 6:
/// <summary> /// This is used to display the list after editing list. /// </summary> private void PrintList(List<CompanyProduct> list, string header) { Response.Write("<br/>"); Response.Write(" " + header); Response.Write("<br/>"); foreach (CompanyProduct cp in list) { //Response.Write(string.Format("Product={0} >>> ProductId={1} >>> CompanyName={2} >>> RegisterDate={3}", // cp.Product, cp.ProductID, cp.CompanyName, cp.ProductRegisterDate)); Response.Write(string.Format(" Product={0} >>> CompanyName={1}", cp.Product, cp.CompanyName)); Response.Write("<br/>"); } Response.Write("<br/>"); } }
Step 7:
class CompanyProduct { public CompanyProduct(string product, int productID, string companyName, DateTime productRegisterDate) { this.product = product; this.productID = productID; this.companyName = companyName; this.productRegisterDate = productRegisterDate; } private string product; public string Product { get { return this.product; } set { this.product = value; } } private int productID; public int ProductID { get { return this.productID; } set { this.productID = value; } } private string companyName; public string CompanyName { get { return this.companyName; } set { this.companyName = value; } } private DateTime productRegisterDate; public DateTime ProductRegisterDate { get { return this.productRegisterDate; } set { this.productRegisterDate = value; } } }