While programming, some times we need to get couple of rows by filtering of a data table.Best option is DataTable.Select Method available in .NET frame work 2.0 or above.
DataTable.Select Method (filter expression )
All you have to do is pass the filter criteria in to the select method then it returns the set of matching data rows. You can use all most every SQL key word to build you filter condition.
E g:
//populate the data table.
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(float));
dt.Columns.Add("Gender of person", typeof(string));
DataTable dtresult = new DataTable();
dtresult = dt.Clone();
for (int i = 0; i <100;i++)
{
DataRow dr = dt.NewRow();
dr["ID"] = i;
dr["Name"] = "Mary" + i.ToString();
dr["Age"] = i;
if (i % 2 == 1)
{
dr["Gender of person"] = "f";
}
else
{
dr["Gender of person"] = "m";
}
dt.Rows.Add(dr);
}
//Retun all the ladies over 50 old.
string strFilerCondition =" [Gender of person]= 'f' AND age>50";
DataRow[] DR = dt.Select(strFilerCondition);
foreach (DataRow dr in DR)
{
dtresult.ImportRow(dr);
}
With in filter condition you can see, I am using square brackets, since the name of the third column “Gender of person” is not a single ward. Otherwise it courses an error.
Gud Artical
ReplyDeleteVisit site www.ebuilders.blogspot.com for SQL beginners