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()
3 comments:
I can't use this one , bcz I didn't have AsEnumerable() in my code , why should I do now?
I had using System.Data.Linq.
Dear Athena,
Please share your both of codes and errors here. those're required for your help.
@Athena: it would help you with ToList() like bellow:
var x = (from y in z select y).ToList();
Post a Comment