LINQ To DataSet 示例
如果在项目遇到这样的问题如:DataTable1和DataTable2需要根据一定的规则进行合并成一个DataTable3。
问题1:DataTable1不是读数据库表的结果,而是合成的数据集,因此无法用SQL语句组合查询。
问题2:DataTable1与DataTable2本身就是非常繁琐的查询且结果集非常大,这样如果DataTable1再与DataTable2
组合查询则很容易发生SQL执行超时。
遇到以上问题,第一个想法就是把两个DataTable,取出放至内存中用嵌套遍历的方式重组得到DataTable3.
.net推出LINQ后就可以用这种更简洁易懂的类SQL语法的LINQ To DataTable 来解决以上问题。下面我例出几个示例分享。
using System.Linq;
using System.Text;
using System.Data;
1.基本方法:
DataSet ds = new DataSet();
adapter.Fill(ds, "Customers");//读数据库略
var result = from s1 in ds.Tables["Customers"].AsEnumerable()
where s1.Field<string>("Region") == "SP" //转换成string后进行对比,如果为DBNull 则传回空字符串
select s1;
foreach (var item in result)
Console.WriteLine(item["CustomerID"]);
Console.ReadLine();
2.指定DataRowVersion:
var result = from s1 in ds.Tables["Customers"].AsEnumerable()
where !s1.IsNull("Region") && (string)s1["Region"] == "SP"
select s1;
3. Join组合查询:
var result = from s1 in ds.Tables["Customers"].AsEnumerable()
join s2 in ds.Tables["Orders"].AsEnumerable() on
s1.Field<string>("CustomerID") equals s2.Field<string>("CustomerID")
where s2.Field<DateTime>("OrderDate") > DateTime.Parse("1997/1/1") &&
s2.Field<DateTime>("OrderDate") < DateTime.Parse("1997/5/31")
select new
{
OrderID = s2.Field<int>("OrderID"),
CustomerName = s1.Field<string>("CompanyName"),
OrderDate = s2.Field<DateTime>("OrderDate")
};
4.Group
var result = from s in ds.Tables["Orders"].AsEnumerable()
group s by s.Field<string>("CustomerID") into g
select new
{
OrderID = g.First().Field<int>("OrderID"),
CustomerID = g.Key
};
5.返回前10行数据
var table = (from s1 in ds.Tables["Customers"].AsEnumerable() select s1).Take(10);
LINQ To DataSet 示例的更多相关文章
- LINQ(LINQ to DataSet)
http://www.cnblogs.com/SkySoot/archive/2012/08/21/2649471.html DataTable.Select()方法使用和 SQL 相似的过滤语法从 ...
- 泛型 Field 和 SetField 方法 (LINQ to DataSet)
LINQ to DataSet 为 DataRow 类提供用于访问列值的扩展方法:Field 方法和 SetField 方法.这些方法使开发人员能够更轻松地访问列值,特别是 null 值.DataSe ...
- LINQ系列:LINQ to DataSet的DataTable操作
LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...
- C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换
LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...
- Linq to DataSet 和 DataSet使用方法学习
简单入门: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...
- LINQ to DataSet,对离线数据的Linq支持、AsEnumeable()
一.DataTable的扩展方法: 1.DataTable转Linq:AsEnumerable 方法 返回IEnumerable<T>对象,其中泛型参数T是DataRow. 此对象可用在 ...
- LINQ系列:LINQ to DataSet的DataView操作
1. 创建DataView EnumerableRowCollection<DataRow> expr = from p in products.AsEnumerable() orderb ...
- Linq To DataSet
private static void LinqToDataSet() { string sql = "select * from Advertising"; using (Dat ...
- LINQ to DataSet的DataTable操作
1. DataTable读取列表 DataSet ds = new DataSet();// 省略ds的Fill代码DataTable products = ds.Tables["Produ ...
随机推荐
- poj 1847( floyd && spfa )
http://poj.org/problem?id=1847 一个水题,用来熟悉熟悉spfa和floyd的. 题意:有m条的铁路,要从x,到y, 之后分别就是条铁路与其他铁路的交点.第一个输入的为有n ...
- poj 2378 (dijkstra)
http://poj.org/problem?id=2387 一个dijkstra的模板题 #include <stdio.h> #include <string.h> #de ...
- 1.JS设计模式-this,call&apply
1. this,call&apply 1.1 this this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用. 1.1.1 普通函数调 ...
- 13.SpringMVC和Spring集成(一) && 14.SpringMVC和Spring集成(二)
1.概念 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,Spring致力于J2EE应用的各层的解决方案,Spring是企业应用开发的“一站式”选择,并贯 ...
- Android图片加载库:最全面的Picasso讲解
前言 上文已经对当今 Android主流的图片加载库 进行了全面介绍 & 对比 如果你还没阅读,我建议你先移步这里阅读 今天我们来学习其中一个Android主流的图片加载库的使用 - Pica ...
- 【编程题目】有两个序列 a,b,大小都为 n,序列元素的值任意整数,无序;(需要回头仔细研究)
32.(数组.规划)有两个序列 a,b,大小都为 n,序列元素的值任意整数,无序:要求:通过交换 a,b 中的元素,使[序列 a 元素的和]与[序列 b 元素的和]之间的差最小.例如: var a=[ ...
- 【python】IP地址处理模块IPy
来源:https://pypi.python.org/pypi/IPy IPy模块 该模块可以方便的处理IPv4和IPv6地址. 以下是从来源中拷贝的一些例子: >>> from I ...
- nginx配置负载
一.系统优化 1.修改/etc/sysctl.conf,优化tcp连接数 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 120 ...
- Yii里获取当前controller和action的id
Yii里获取当前controller和action的id 在控制器里$name = $this->getId(); // controller$name = $action->id; ...
- iOS第三方分享-ShareSDK
网址链接:http://mob.com/Download/detail?type=1&plat=2 由于新版的简单分享很多功能都有,而且打包后体积比全版本的少了几M所以在这里用这个 1.在官网 ...