其他分享
首页 > 其他分享> > LINQ笔记-LINQ操作DataTable

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()};

标签:Name,CompanyID,笔记,ID,Add,LINQ,table,DataTable
来源: https://www.cnblogs.com/fanfan-90/p/12114705.html