Monday, November 17, 2014

How to query datatable in linq

  LInq consistent model for working with data across various kinds of data sources and formats.  In this example how to use linq query on datatable. Here is example given below. In this example I have created the datatable and containg the column EmpName and Age. So   I converted datatable to enummerabe to list type to query.  T
//x.Field<string>("age")
To access column linq you have to use above syntax
X belong the current  object.
Field<string>("age") this is column type and column name
public ActionResult EmpList(string CategoryName)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("EmpName");
            dt.Columns.Add("Age");

            DataRow row1 = dt.NewRow();
            row1["Name"] = "anil";
            row1["Age"] = "30";
            dt.Rows.Add(row1);

            DataRow row2 = dt.NewRow();
            row1["Name"] = "sunil";
            row1["Age"] = "34";
            dt.Rows.Add(row2);

            var query = dt.AsEnumerable().SingleOrDefault(x => x.Field<string>("age") == 30");

            return View();
        }

No comments:

Post a Comment

http://blogsiteslist.com