Thursday, May 29, 2014

Get the Specific Row From the Data Table using LINQ in Asp.Net

Step 1:- Create One dynamic datatable

DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(int));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Rows.Add(1, "Lokesh", "B.Tech");
dt.Rows.Add(2, "Vijay", "Msc");
dt.Rows.Add(3, "Balaji", "MS");
dt.Rows.Add(4, "Charan", "B.Tech");
dt.Rows.Add(5, "Durga", "MD");
dt.Rows.Add(6, "Shiva", "B.Tech");
dt.Rows.Add(7, "Venkat", "CA");

This is the LINQ code for getting the specific row based on the Userid

var results = (from myRow in dt.AsEnumerable()
where myRow.Field<int>("UserId") == Convert.ToInt16(“1”)
                               select myRow).CopyToDataTable();

GridView1.DataSource = results;
GridView1.DataBind();


No comments:

Post a Comment