LINQ系列:LINQ to DataSet的DataTable操作

LINQ to DataSet需要使用System.Core.dll、System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System.Data和System.Data.DataSetExtensions。1. DataTable读取列表DataSet ds =newDataSet();//省略ds的Fill代码DataTable products = ds.Tables[“Product”];IEnumerable rows =from p in products.AsEnumerable()select p;foreach(DataRow row in rows){Console.WriteLine(row.Field(“ProductName”));}DataSet ds =newDataSet();//省略ds的Fill代码DataTable products = ds.Tables[“Product”];var rows =products.AsEnumerable().Select(p=>new{ProductID= p.Field(“ProductID”),ProductName= p.Field(“ProductName”),UnitPrice= p.Field(“UnitPrice”)});foreach(var row in rows){Console.WriteLine(row.ProductName);}var products = ds.Tables[“Product”].AsEnumerable();var query =from p in products select p.Field(“ProductName”);2. DataTable查询var rows =products.AsEnumerable().Where(p=> p.Field(“UnitPrice”) >10m).Select(p=>new{ProductID= p.Field(“ProductID”),ProductName= p.Field(“ProductName”),UnitPrice= p.Field(“UnitPrice”)});3. DataTable数据排序var rows =products.AsEnumerable().Where(p=> p.Field(“UnitPrice”) >10m).OrderBy(p=> p.Field(“SortOrder”)).Select(p=>new{ProductID= p.Field(“ProductID”),ProductName= p.Field(“ProductName”),UnitPrice= p.Field(“UnitPrice”)});var expr =from p in products.AsEnumerable()                order by p.Field(“SortOrder”)                select p;IEnumerable rows =expr.ToArray();foreach(var row in rows){Console.WriteLine(row.Field(“ProductName”));}var expr =from p in ds.Tables[“Product”].AsEnumerable()                 orderby p.Field(“SortOrder”), p.Field(“ProductName”) descending                  select p;4. 多个DataTable查询var query =from p in ds.Tables[“Product”].AsEnumerable()                   from c in ds.Tables[“Category”].AsEnumerable()                   where p.Field(“CategoryID”) == c.Field(“CategoryID”) &&                               p.Field(“UnitPrice”) >10m                     select new{                                      ProductID= p.Field(“ProductID”),                                      ProductName= p.Field(“ProductName”),                                      CategoryName= c.Field(“CategoryName”)                    };5. DataTable分组var query =from p in ds.Tables[“Product”].AsEnumerable()                 group p by p.Field(“CategoryID”) into g                 select new{                                  CategoryID=g.Key,                                  Products=g                };foreach(var item in query){    Console.WriteLine(item.CategoryID);    foreach(var p in item.Products)   {          Console.WriteLine(p.Field(“ProductName”));   }}查询Product中每个CategoryID的数目:var expr =from p inds.Tables[“Product”].AsEnumerable()                group p by p.Field(“CategoryID”) into g                 select new{                                 CategoryID=g.Key,                                 ProductsCount=g.Count()                 };6. 多个DataTable左连接var linq1 = from a in dt1.AsEnumerable()                                          join b in dt2.AsEnumerable()                   on a.Field(“sys_nation_id”) equals b.Field(“sys_nation_id”)                   into ab                                          from bb in ab.DefaultIfEmpty()                                           select new {                                                                proname = a.Field(“name”),                                                                countryname = bb == null ? “” : bb.Field(“name”)                   };若要实现三个表左连接查询:var linq2 = from a in dt1.AsEnumerable()                  join b in dt2.AsEnumerable()                 on a.Field(“sys_nation_id”) equals b.Field(“sys_nation_id”)                 into ab                 from bb in ab.DefaultIfEmpty()                 join c in dt3.AsEnumerable()                 on a.Field(“sys_city_id”) equals b.Field(“sys_city_id”)                 into bc                 from cc in bc.DefaultIfEmpty()                 select new {                                        proname = a.Field(“name”),                                        countryname = bb == null ? “” : bb.Field(“name”),                                        cityname = cc == null ? “” : cc.Field(“name”)                  };

作者:魏明山
链接:https://www.jianshu.com/p/8219371a42d3
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值