How LINQ works with DataTable
Why we can not query against the DataTable's Rows collection?
I can not find AsEnumerable() in my DataTable
Sample
DataTable Student = new DataTable("Student"); Student.Columns.Add("Name", typeof(String)); Student.Columns.Add("Date", typeof(DateTime)); Student.Columns.Add("NO", typeof(Int32)); Student.Rows.Add(new object[] {"A1",Convert.ToDateTime("2010/01/01"),0}); Student.Rows.Add(new object[] {"A2",Convert.ToDateTime("2011/01/01"),0}); Student.Rows.Add(new object[] {"A3",Convert.ToDateTime("2009/01/01"),1}); Student.Rows.Add(new object[] {"A4",Convert.ToDateTime("2012/01/01"),1}); Student.Rows.Add(new object[] {"A5",Convert.ToDateTime("2011/01/01"),1}); Student.Rows.Add(new object[] {"A6",Convert.ToDateTime("2011/01/01"),2}); Student.Rows.Add(new object[] {"A7",Convert.ToDateTime("2011/01/01"),2}); // Linq Query var query = from rows in Student.AsEnumerable() where rows["NO"].Equals(1) orderby rows["Date"] descending select rows; // A DataTable result = query.CopyToDataTable(); MessageBox.Show(result.Rows[0][0].ToString()); // B MessageBox.Show(query.AsDataView()[0][0].ToString());
PS: If you want to use of LINQ to DataSet as ORM you can used of ToQueryable() instead of AsEnumerable().
PS: If you want to use of EF (ADO.NET Entity Framework) as ORM you can directrly use of your Table instead of ToQueryable() or AsEnumerable()