AsEnumerable()
DataTable table = GetTable(); var q = from n in table.AsEnumerable() select new Person { ID=n.Field<int>("ID"), Name = n.Field<string>("Name") , CompanyID = n.Field<int>("CompanyID") };
public static DataTable GetTable() { DataTable table = new DataTable(); table.Columns.Add("ID",typeof(int)); table.Columns.Add("Name",typeof(string)); table.Columns.Add("CompanyID", typeof(int)); DataRow row1 = table.NewRow(); row1["ID"]=1; row1["Name"] = "zhangsan"; row1["CompanyID"] = 1; table.Rows.Add(row1); DataRow row2 = table.NewRow(); row2["ID"]= 2; row2["Name"]= "lisi"; row2["CompanyID"] = 1; table.Rows.Add(row2); return table; }
分组
var q = from n in table.AsEnumerable() group n by n.Field<int>("CompanyID") into g orderby g.Key descending select new { CompanyID= g.Key,List=g.ToList()};