首页 > 代码库 > Linq 查询

Linq 查询

            var dt = new DataTable();
            var list = dt.AsEnumerable();
            var qurey = from tmp in list
                        where 1 == 1
                        select new ent()
                        {
                            Id = tmp.Field<string>("Id"),
                            Name = tmp.Field<string>("Name")
                        };
            qurey.ToList();


        public class ent
        {
            public string Id { getset; }
            public string Name { getset; }
        }

//优化

  var dt = new DataTable();
        dt.Columns.Add("Id");
        dt.Columns.Add("Name");
        dt.Columns.Add("Addre");
        DataRow dr = dt.NewRow();
        dr["Id"] = "01";
        dr["Name"] = "King";
        dr["Addre"] = "addreeeeeee";
        dt.Rows.Add(dr);
        
        var list = dt.AsEnumerable();
        var qurey = list.Select(tmp => new
        {
            Id = tmp.Field<string>("Id"),
            Name = tmp.Field<string>("Name")
        });
        var lis = qurey.ToList();
        var dtTmp = ToDataTable(lis);



来自为知笔记(Wiz)


Linq 查询