Step 1: Used Namespaces:
using
System;
using
System.Collections.Generic;
Step 2: Used Collection for demo:
//
<summary>
///
This is static collection used in this tutorials
///
</summary>
///
<returns></returns>
private
List<CompanyProduct>
AddCollection()
{
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: Printing the collection:
List<CompanyProduct>
list = AddCollection();
PrintList(list,
"Available Collection");
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"; });
Response.Write("<br/>");
Response.Write(string.Format("
Product={0} >>> CompanyName={1}",
cp.Product, cp.CompanyName));
Step 4: Filtering the first product by product id using Predicate
//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: Filtering the first product by productRegisterDate using
Predicate
//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: Used for printing the results:
///
<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: Used Class for Demo
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; }
}
}
Thanks for reading this blog. Have a nice day.