datatable linq查询
类别:编程学习 浏览量:2637
时间:2015-10-29 datatable linq查询
datatable linq查询DataTable通过调用AsEnumerable()方法,从而运用Linq查询。其中AsEnumerable方法在System.Data.DataSetExtensions.dll中定义,一般VS会自动引用这个dll。
一、datatable linq查询实例
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 linq 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")
});
3、DataTable linq 数据排序
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") descending
select p;
4、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"));
}
}
查询Product中每个CategoryID的数目
var expr = from p in ds.Tables["Product"].AsEnumerable()
group p by p.Field<int>("CategoryID") into g
select new
{
CategoryID = g.Key,
ProductsCount = g.Count()
};
5、多个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")
};
二、linq 对象转换为DataTable
通过CopyToDataTable()方法
DataTable newD1t = query1.CopyToDataTable<DataRow>();
foreach (DataRow item in newD1t.Rows)
{
System.Console.WriteLine(item["Name"]);
}
标签:linq
您可能感兴趣
- LINQ中Aggregate的用法
- 使用 LINQPad 调试linq以及lambda表达式
- linq not in 查询
- linq中let
- linq中AsEnumerable和AsQueryable的区别
- linq中延迟执行
- Linq中的TakeWhile和SkipWhile
- Linq中select查询
- Linq操作Datable
- list使用linq排序
- linq distinct去重
- LINQ TO SQL 中join
- linq to sql 中Concat、Union、Intersect、Except
- linq中group by
- linq 排序
- linq 数据类型转换
- 学好汉语拼音,从娃娃绕口令抓起,平时还是要多练 收藏好(从娃娃绕口令抓起)
- 仙女们的私藏鲜法大PK 鲜香切块牛肉(仙女们的私藏鲜法大PK)
- 天热没胃口 这道菜开胃又下饭,2个小技巧新手一学就会(这道菜开胃又下饭)
- 指天椒紫苏爆炒牛肉(指天椒紫苏爆炒牛肉)
- 谷雨前,吃牛羊肉别忘了吃河鲜,除湿还清热,加紫苏一炒特解馋(吃牛羊肉别忘了吃河鲜)
- 紫苏牛肉锅里滚一滚,香的鼻子都要掉了(紫苏牛肉锅里滚一滚)
热门推荐
- jquery中append和appendto的区别
- 单元格内容过多超出怎么办(单元格内文本显示超过单元格宽度的快速解决办法)
- docker实现redis集群(详解使用Docker进行Redis主从复制实践)
- apache搭建ftp服务器(使用Apache&花生壳架设Web服务器)
- 安装SQL Server 2016出错提示:需要安装oracle JRE7 更新 51(64位)或更高版本问题的解(安装SQL Server 2016出错提示:需要安装oracle JRE7 更新 5164位或更高版本问题的解)
- php面向对象如何开发(PHP创建对象的六种方式实例总结)
- dedecms获取子目录(详解dedecms利用[field:arcurl/]无法获取文章路径的解决方法)
- WinForm窗体间如何传值
- windows下nginx负载均衡配置(使用nginx配置访问wgcloud的方法)
- js复制内容到剪贴板