如果在项目遇到这样的问题如: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. Bloom Filter 概念和原理

    Bloom filter 是由 Howard Bloom 在 1970 年提出的二进制向量数据结构,它具有很好的空间和时间效率,被用来检测一个元素是不是集合中的一个成员.如果检测结果为是,该元素不一定 ...

  2. phpcms模板标签整理

    {template "content","header"} 调用根目录下phpcms\template\content\header文件 {CHARSET} 字 ...

  3. git中找回丢失的对象

    本文转载自:http://gitbook.liuhui998.com/5_9.html 译者注: 原书这里只有两个链接: Recovering Lost Commits Blog Post,Recov ...

  4. android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法

    android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法 原方法: public static Bitmap getSmallBitmap(Strin ...

  5. [Android]检查当前手机是否有网络

    // Check network connection private boolean isNetworkConnected(){ ConnectivityManager connectivityMa ...

  6. nyoj412_bitset_

    Same binary weight 时间限制:300 ms  |  内存限制:65535 KB 难度:3   描述 The binary weight of a positive  integer ...

  7. VC++ 获取当前模块的路径(dll/exe)

    一般地,获取当前模块路径都是通过调用 GetModuleFileName() 来获取的. DWORD WINAPI GetModuleFileName( __in HMODULE hModule, _ ...

  8. codeforces 492C. Vanya and Exams 解题报告

    题目链接:http://codeforces.com/problemset/problem/492/C 题目意思:给出 3 个整数:n,  r,  avg.然后有 n 行,每行有两个数:第 i 行有 ...

  9. android学习————项目导入常见错误整理(转载)

    详细请访问http://4789781.blog.51cto.com/4779781/1031107

  10. PDO(数据访问抽象层)

    自带事务功能,多条sql同时执行时,如果其中一条执行失败,那么所有的都执行失败.开启了事务,可以进行回滚操作,让程序变得更安全. 1.访问不同的数据库2.自带事务功能3.防止SQL注入:分两次发送 / ...