Helle sir/madam
i want to filter datatable. for that i am using linq in this way its not working its give an error " The source contains no DataRows."
first i get a value in a datatable dt.
now i am using Linq
var query = from r in dt.AsEnumerable()
where r.Field("AI_No_Of_Conception") == ""
select r;
DataTable dtLink=new DataTable();
dtLink = query.CopyToDataTable();
if any another option how to apply a query on datatable and bind it another Datatable then please suggest my.
Hemant SrivastavaPosted Nov 29, 2012, 10:40 AM
Otherwise you can use lambda expression over the data table easily.. Refer to following link:
http://www.c-sharpcorner.com/UploadFile/0f68f2/querying-a-data-table-using-select-method-and-lambda-express/
Hemant SrivastavaPosted Dec 2, 2012, 10:53 AM
-Hemant
Rohit SharmaPosted Dec 2, 2012, 2:27 AM
due to you help i solved my problem.
I done how to filter datatable and bind it another datatable,as follow
public void Linq()
{
int i=0;
DataTable dtt=new DataTable();
dtt.Columns.Add("SSN", typeof(string));
dtt.Columns.Add("NAME", typeof(string));
dtt.Columns.Add("ADDR", typeof(string));
dtt.Columns.Add("AGE", typeof(int));
dtt.Rows.Add();
DataTable dt = new DataTable();
dt.Columns.Add("SSN", typeof(string));
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("ADDR", typeof(string));
dt.Columns.Add("AGE", typeof(int));
// Showing how to set Primary Key(s) in a Data table (Although it's not compulsory to have one)
DataColumn[] keys = new DataColumn[1];
keys[0] = dt.Columns[0];
dt.PrimaryKey = keys;
dt.Rows.Add("203456876", "John", "12 Main Street, Newyork, NY", 15);
dt.Rows.Add("203456877", "SAM", "13 Main Ct, Newyork, NY", 25);
dt.Rows.Add("203456878", "Elan", "14 Main Street, Newyork, NY", 35);
dt.Rows.Add("203456879", "Smith", "12 Main Street, Newyork, NY", 45);
dt.Rows.Add("203456880", "SAM", "345 Main Ave, Dayton, OH", 55);
dt.Rows.Add("203456881", "Sue", "32 Cranbrook Rd, Newyork, NY", 65);
dt.Rows.Add("203456882", "Winston", "1208 Alex St, Newyork, NY", 65);
dt.Rows.Add("203456883", "Mac", "126 Province Ave, Baltimore, NY", 85);
dt.Rows.Add("203456884", "SAM", "126 Province Ave, Baltimore, NY", 95);
// DataRow[] DataTable.Select(string filterExpression, string sort)
foreach (DataRow o in dt.Select("NAME <> 'SAM'"))
{
// Console.WriteLine("\t" + o["SSN"] + "\t" + o["NAME"] + "\t" + o["ADDR"] + "\t" + o["AGE"]);
string aa = o["SSN"].ToString();
string ab = o["NAME"].ToString();
string ac = o["ADDR"].ToString();
string ad = o["AGE"].ToString();
//for (int i = 0; i < dt.Rows.Count; i++)
//{
dtt.Rows[i][0] = aa;
dtt.Rows[i][1] = ab;
dtt.Rows[i][2] = ac;
dtt.Rows[i][3] = ad;
dtt.Rows.Add();
//}
//dtt.ImportRow(dt.Rows[0]);
i = i + 1;
}
}
so filter value store in dtt table.
Hemant SrivastavaPosted Nov 30, 2012, 11:14 AM
dt.Select("AI_Status IS NULL")
Hope it helps!
Please maek it accepted if it solves you problem.
Rohit SharmaPosted Nov 30, 2012, 2:35 AM
so plz can you suggest me.
Rohit SharmaPosted Nov 29, 2012, 11:45 PM