LINQ笔记-LINQ操作DataTable

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()};
上一篇:泛型list集合类转换成DataTable


下一篇:public DataTable ExecuteQuery(string sql) 这段话具体意思