Friday, June 4, 2010

FIND() AND FINDALL() FUNCTIONS IN LINQ

We have a table Customer which contains details of a customer like name, address, phone, etc.

we have a list of customers -
List lstCust = new List();

we want to find a particular record from this list based on some criteria.
say where name = 'abc';

In C# :

List lstCustResult = lstCust .FindAll(
delegate(Customers cust)
{
return cust.name == "abc";
});

This will return all the records having name ="abc".

suppose we want to fetch a particular value based on some criteria, in that case we will use Find instead of FindAll.

string name = lstCust .Find(
delegate(Customers cust)
{
return cust.id== 1;
}).name;

Also, if suppose we have a list of strings and want to find a particular record in that list.
For That we need to do something like this :

List lstNumbers = new List();
lstNumbers.Add("One");
lstNumbers.Add("Two");
lstNumbers.Add("Three");
lstNumbers.Add("Four");
lstNumbers.Add("One");
List result= lst.FindAll(delegate(string name)
{
name = "One";
return lstNumbers.Equals(name);
});

Bookmark and Share

No comments:

Post a Comment