Linq操作Datable
类别:编程学习 浏览量:694
时间:2017-8-28 Linq操作Datable
Linq操作Datable一、Datable简单查询
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);
}
二、Datable代where过滤条件的查询
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")
});
三、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 ds.Tables["Product"].AsEnumerable()
orderby p.Field<int>("SortOrder"), p.Field<string>("ProductName") descending
select p;
四、多个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") > 10m
select new
{
ProductID = p.Field<int>("ProductID"),
ProductName = p.Field<string>("ProductName"),
CategoryName = c.Field<string>("CategoryName")
};
五、DataTable分组
var query = from p in ds.Tables["Product"].AsEnumerable()
group p by p.Field<int>("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<string>("ProductName"));
}
}
您可能感兴趣
- linq中let
- Linq中的TakeWhile和SkipWhile
- linq not in 查询
- linq中延迟执行
- linq 数据类型转换
- Linq中where查询
- Linq操作Datable
- LINQ TO SQL 中join
- linq 排序
- linq distinct去重
- linq中group by
- linq中AsEnumerable和AsQueryable的区别
- Linq中select查询
- linq to sql 中Concat、Union、Intersect、Except
- list使用linq排序
- LINQ中Aggregate的用法
- 数字藏品市场有多乱 周杰伦丢了 一只猴 ,损失超300万(数字藏品市场有多乱)
- 这里输入关键词(怎么输入关键词搜索)
- 得这个 难治病 的人太多了,300个人赶到杭州商量怎么办(得这个难治病的人太多了)
- 经度,世界时间腕表的灵魂(世界时间腕表的灵魂)
- 阿里最新财报公布 三季度营收增长3 ,将增加150亿美元回购额度 在美股价小涨(阿里最新财报公布)
- 赵薇时胖时瘦 最近变美少女 原因在这里 躺着就变瘦(赵薇时胖时瘦最近变美)
热门推荐
- php解压压缩包(PHP 实现文件压缩解压操作的方法)
- dedecms安装配置(浅析织梦dedecms三个最重要的安全设置)
- docker的postgres镜像包(Docker中运行PostgreSQL并推荐几款连接工具)
- docker容器镜像内容(Docker容器从镜像恢复代码的操作步骤)
- php常见加密方法(PHP实现的AES 128位加密算法示例)
- 如何选择适合自己的云服务器(选择云服务器配置需要注意什么?)
- linux服务器安装宝塔(linux宝塔面板安装Zabbix监控服务器资源使用状态)
- php编程输出菱形(php输出反斜杠的实例方法)
- python处理时间序列常用方法汇总(python整小时 整天时间戳获取算法示例)
- python比go语言简单(Python和Go语言的区别总结)