Search This Blog

Tuesday, March 25, 2014

Filter with LINQ DataTable C#

C# > System.Data   > DataTable > Filter with LINQ

Example:


DataTable dt = new DataTable("table");
dt.Columns.Add("id", Type.GetType("System.Int32"));
dt.Columns.Add("name", Type.GetType("System.String"));

DataRow dr = dt.NewRow();
dr["id"] = 1;
dr["name"] = "john";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["id"] = 2;
dr["name"] = "dan";
dt.Rows.Add(dr);

DataTable tbl = (from DataRow dr1 in dt.Rows
                     where dr1["id"].ToString() == "1"
                     select dr).CopyToDataTable();