日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换

發布時間:2024/4/15 C# 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

LINQ to DataSet需要使用System.Core.dll、System.Data.dll和System.Data.DataSetExtensions.dll,在項目中添加引用System.Data和System.Data.DataSetExtensions。

1. DataTable讀取列表

DataSet ds = new DataSet(); // 省略ds的Fill代碼 DataTable products = ds.Tables["Product"]; IEnumerable<DataRow> rows = from p in products.AsEnumerable()select p; foreach (DataRow row in rows) {Console.WriteLine(row.Field<string>("ProductName")); } DataSet ds = new DataSet(); // 省略ds的Fill代碼 DataTable products = ds.Tables["Product"]; var rows = products.AsEnumerable().Select(p => new{ProductID = p.Field<int>("ProductID"),ProductName = p.Field<string>("ProductName"),UnitPrice = p.Field<decimal>("UnitPrice")}); foreach (var row in rows) {Console.WriteLine(row.ProductName); } var products = ds.Tables["Product"].AsEnumerable(); var query = from p in productsselect p.Field<string>("ProductName");

2. DataTable查詢

var rows = products.AsEnumerable().Where(p => p.Field<decimal>("UnitPrice") > 10m).Select(p => new{ProductID = p.Field<int>("ProductID"),ProductName = p.Field<string>("ProductName"),UnitPrice = p.Field<decimal>("UnitPrice")});

3. DataTable數據排序

var rows = products.AsEnumerable().Where(p => p.Field<decimal>("UnitPrice") > 10m).OrderBy(p => p.Field<int>("SortOrder")).Select(p => new{ProductID = p.Field<int>("ProductID"),ProductName = p.Field<string>("ProductName"),UnitPrice = p.Field<decimal>("UnitPrice")}); var expr = from p in products.AsEnumerable()orderby p.Field<int>("SortOrder")select p; IEnumerable<DataRow> rows = expr.ToArray(); foreach (var row in rows) {Console.WriteLine(row.Field<string>("ProductName")); } var expr = from p in ds.Tables["Product"].AsEnumerable()orderby p.Field<int>("SortOrder"), p.Field<string>("ProductName") descendingselect p;

4. 多個DataTable查詢

var query = from p in ds.Tables["Product"].AsEnumerable()from c in ds.Tables["Category"].AsEnumerable()where p.Field<int>("CategoryID") == c.Field<int>("CategoryID")&& p.Field<decimal>("UnitPrice") > 10mselect new{ProductID = p.Field<int>("ProductID"),ProductName = p.Field<string>("ProductName"),CategoryName = c.Field<string>("CategoryName")};

5. DataTable分組

var query = from p in ds.Tables["Product"].AsEnumerable()group p by p.Field<int>("CategoryID") into gselect 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<string>("ProductName"));} }

查詢Product中每個CategoryID的數目:

var expr = from p in ds.Tables["Product"].AsEnumerable()group p by p.Field<int>("CategoryID") into gselect new{CategoryID = g.Key,ProductsCount = g.Count()};

?

DataTable與Linq相互轉換

DataTable通過dt.AsEnumerable()方法轉換可用Linq查詢,反之,Linq也可以轉化為DataTable
DataTable newDt = query1.CopyToDataTable<DataRow>();
var query1 =
??? from stu in dtStu.AsEnumerable()
??? from score in dtScore.AsEnumerable()
??? where stu.Field<int>("ScoreID") == score.Field<int>("ScoreID")
??? where (int)stu["Age"] > 20
??? select stu;


//通過CopyToDataTable()方法創建新的副本
DataTable newDt = query1.CopyToDataTable<DataRow>();
?foreach (var item in newDt.AsEnumerable())??
?{??
???? System.Console.WriteLine(item["Name"]);??
}

?

轉:LINQ系列:LINQ to DataSet的DataTable操作

DataTable與Linq相互轉換

?

轉載于:https://www.cnblogs.com/wangfuyou/p/6956759.html

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。