如果在项目遇到这样的问题如: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 示例的更多相关文章

  1. LINQ(LINQ to DataSet)

    http://www.cnblogs.com/SkySoot/archive/2012/08/21/2649471.html DataTable.Select()方法使用和 SQL 相似的过滤语法从 ...

  2. 泛型 Field 和 SetField 方法 (LINQ to DataSet)

    LINQ to DataSet 为 DataRow 类提供用于访问列值的扩展方法:Field 方法和 SetField 方法.这些方法使开发人员能够更轻松地访问列值,特别是 null 值.DataSe ...

  3. LINQ系列:LINQ to DataSet的DataTable操作

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

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

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

  5. Linq to DataSet 和 DataSet使用方法学习

    简单入门: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...

  6. LINQ to DataSet,对离线数据的Linq支持、AsEnumeable()

    一.DataTable的扩展方法: 1.DataTable转Linq:AsEnumerable 方法 返回IEnumerable<T>对象,其中泛型参数T是DataRow. 此对象可用在 ...

  7. LINQ系列:LINQ to DataSet的DataView操作

    1. 创建DataView EnumerableRowCollection<DataRow> expr = from p in products.AsEnumerable() orderb ...

  8. Linq To DataSet

    private static void LinqToDataSet() { string sql = "select * from Advertising"; using (Dat ...

  9. LINQ to DataSet的DataTable操作

    1. DataTable读取列表 DataSet ds = new DataSet();// 省略ds的Fill代码DataTable products = ds.Tables["Produ ...

随机推荐

  1. Python自动化之线程进阶篇(二)

    queue队列 class queue.Queue(maxsize=0) #先入先出 class queue.LifoQueue(maxsize=0) #后入先出 class queue.Priori ...

  2. centos mysql 大量数据导入时1153 错误:1153 - Got a packet bigger than 'max_allowed_packet' bytes

    参考:http://stackoverflow.com/questions/93128/mysql-error-1153-got-a-packet-bigger-than-max-allowed-pa ...

  3. 【架构】How To Use HAProxy to Set Up MySQL Load Balancing

    How To Use HAProxy to Set Up MySQL Load Balancing Dec  2, 2013 MySQL, Scaling, Server Optimization U ...

  4. poj 2240(floyd)

    http://poj.org/problem?id=2240 题意:有些人会利用货币的不用汇率来进行套现,比如1美元换0.5英镑,而1英镑又可以换10法郎,而1法郎又可以换0.21的美元,那么经过货币 ...

  5. Calendar类测试

    public static void main(String[] args) throws ParseException { // 字符串转换日期格式 // DateFormat fmtDateTim ...

  6. 基于Maven构建开发第一个Storm项目

    前面说过了Storm的测试项目,那么此时我们更想自己写一个小项目来练练手,首先我们自己的Windows系统上首先应该安装好maven,然后启动Eclipse for JavaEE版本,接下来开始建立项 ...

  7. Python 生产环境MySQL数据库增量备份脚本

    MySQL数据库常用的办法是通过MySQLdump导出sql进行备份,但是不适合数据量很大的数据库,速度,锁表是两个严重的问题.前面写了一遍文章介绍xtrabackup的热备工具,见 http://w ...

  8. 常用iOS第三方库以及XCode插件介绍

    第三方库 CocoaPod CocoaPod并不是iOS上的第三方库 而是大名鼎鼎的第三方库的管理工具 在CocoaPod没有出现之前 第三方库的管理是非常痛苦的 尤其是一些大型的库(比如nimbus ...

  9. 数据结构-链表逆置(c++模板类实现)

    链表结点类模板定义: template <class T> class SingleList; template <class T> class Node { private: ...

  10. JSON 数据格式

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于人阅读和编写,同时也易 ...