标签:style blog color io ar 使用 for strong sp
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); }
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")); }
LINQ系列:LINQ to DataSet的DataTable操作
标签:style blog color io ar 使用 for strong sp
原文地址:http://www.cnblogs.com/libingql/p/4045807.html