public static DataTable DatableInnerJoin(DataTable FirstTB, DataTable SecondTB, DataColumn[] FJC, DataColumn[] SJC)
{
if (FirstTB == null || SecondTB == null)
return null;
if (FJC == null || FJC.Length <= )
return null;
if (SJC == null || SJC.Length <= )
return null;
DataTable table = new DataTable("Join");
using (DataSet ds = new DataSet())
{
ds.Tables.AddRange(new DataTable[] { FirstTB.Copy(), SecondTB.Copy() });
DataColumn[] parentcolumns = new DataColumn[FJC.Length];
for (int i = ; i < parentcolumns.Length; i++)
{
parentcolumns[i] = ds.Tables[].Columns[FJC[i].ColumnName];
}
DataColumn[] childcolumns = new DataColumn[SJC.Length];
for (int i = ; i < childcolumns.Length; i++)
{
childcolumns[i] = ds.Tables[].Columns[SJC[i].ColumnName];
}
DataRelation relation = new DataRelation("Relation_Table", parentcolumns, childcolumns, false);
ds.Relations.Add(relation);
for (int i = ; i < FirstTB.Columns.Count; i++)
{
table.Columns.Add(FirstTB.Columns[i].ColumnName, FirstTB.Columns[i].DataType);
} for (int i = ; i < SecondTB.Columns.Count; i++)
{
if (!table.Columns.Contains(SecondTB.Columns[i].ColumnName))
table.Columns.Add(SecondTB.Columns[i].ColumnName, SecondTB.Columns[i].DataType);
else
table.Columns.Add(SecondTB.Columns[i].ColumnName + "_Second", SecondTB.Columns[i].DataType);
}
table.BeginLoadData();
foreach (DataRow firstrow in ds.Tables[].Rows)
{
DataRow[] childrows = firstrow.GetChildRows(relation);
if (childrows != null && childrows.Length > )
{
object[] parentarray = firstrow.ItemArray;
foreach (DataRow secondrow in childrows)
{
object[] secondarray = secondrow.ItemArray;
object[] joinarray = new object[parentarray.Length + secondarray.Length];
Array.Copy(parentarray, , joinarray, , parentarray.Length);
Array.Copy(secondarray, , joinarray, parentarray.Length, secondarray.Length);
table.LoadDataRow(joinarray, true);
}
}
}
table.EndLoadData();
}
return table;
}

2个或多个datable类似于sql inner join 合并查询的更多相关文章

  1. C#EF中,使用类似于SQL中的% 模糊查询

    最近在做项目的时候需要使用到模糊查询,但是后台使用EF写的 而不是ADO或者是Dapper,如果是这样的话,我们就可以使用Sql语句直接进行模糊查询 现在我们需要在LINQ中使用类似于模糊查询 在EF ...

  2. SQL使用union合并查询结果(转载)

    1.UNION的作用  UNION 指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION 的一 ...

  3. SQL中join连接查询时条件放在on后与where后的区别

    数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成临时表时使用的条 ...

  4. SQL多表合并查询结果

    两表合并查询,并同时展示及分页SELECT a.* FROM ( ( SELECT punycode, `domain`, 'Success' AS state, add_time, AS refun ...

  5. LINQ语法类似于SQL的语法

    LINQ语法类似于SQL的语法如下, Models.BookStoreEntities 是从添加新建项中的数据--->ADO.NET实体数据模型--->从数据库生成--->使用5.0 ...

  6. NSPredicate用法总结(Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取)

    简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取. 定义(最常用到的方法): NSPredicate *ca = [NSPred ...

  7. 图解SQL的Join 转自coolshell

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  8. SQL多表连接查询

    SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student  截图如下: 表2:course  截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际 ...

  9. 图解SQL的Join(转)

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

随机推荐

  1. 在IIS集成管道中使用OWIN Middleware

    在Katana中启用Windows Authorization OWIN的架构: Host 管理OWIN pipeline上运行的进程 Server 打开一个network socket,,监听请求 ...

  2. .Net调用Office Com组件的原理及问题检索com类工厂组件检索 COM 类工厂中 CLSID 为 {XXX} 的组件失败

    我是在本地32位操作系统+vs2010+office2007做创建并下载Excel,ppt文件的操作没有问题,发布到64位系统的服务器上报错,最开始报错:: 1:Retrieving the COM ...

  3. java内部类实现多继承

    class Example1 { public String name() { return "liutao"; } } class Example2 { public int a ...

  4. HLS(HTTP Live Streaming)协议之m3u8文件生成方式

    HLS(HTTP Live Streaming)是Apple的动态码率自适应技术.主要用于PC和Apple终端的音视频服务.包括一个m3u(8)的索引文件,TS媒体分片文件和key加密串文件. HLS ...

  5. hdu 1258 DFS

    I - 深搜 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bi ...

  6. C++中的cout输出机制

    代码: #include <iostream> using namespace std; int hello(){ cout<<"hello"<< ...

  7. 在JasperReport中填充JavaBean(4)

    使用Parameters参数对象传递字符串的示例,本节将演示打印List接口中Userinfo.java实体类的示例,打印的数据源不是来自于Parameters对象,而是JRBeanCollectio ...

  8. 在Ubuntu上安装VmTools

    1.添加VmTools 2.解压 .tag.gz文件 使用Linux命令: tar –zxvf src –c dis -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 ...

  9. nodeclub 学习记录

    源码地址:https://github.com/cnodejs/nodeclub 按照 它的步骤 在系统中跑没有出错,但是注册后没有发送邮件验证码,我将 controller层下面的sign.js 的 ...

  10. [POJ] 2456 Aggressive cows (二分查找)

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...