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. word-wrap 和 word-break

    一.word-wrap 1.浏览器支持 所有主流浏览器都支持 word-wrap属性 2.定义和用法 word-wrap 属性允许长单词或 URL 地址换行到下一行. 语法 word-wrap: no ...

  2. java基础2

    //第一个程序 用super访问父类中被隐藏的成员变量和被重写的方法 package foxe; class superClass{ int x; superClass(){ x=4; System. ...

  3. DTO学习系列之AutoMapper(四)

    本篇目录: Mapping Inheritance-映射继承 Queryable Extensions (LINQ)-扩展查询表达式 Configuration-配置 Conditional Mapp ...

  4. vc远程调试启动进程(非attach)

    被调试端设置同attach进程方式的远程调试 代码端,需要在[Project] [Properties] [Configuration Properties] [Debugging].将Debugge ...

  5. css3之gradient

    radial-gradient The CSS radial-gradient() function creates an <image> which represents a gradi ...

  6. 【干货】.NET开发通用组件发布(一) 介绍

    组件介绍 集合个人和团都开发中遇到的一些通用组件,邮件发送组件.内容采集.CSV数据文件导入工具.日志记录组件.MVC验证登陆组件.MVC分页组件.短信发送组件和强大的Repeate和Repeater ...

  7. Bootstrap学习笔记(未整理)

    强调class 这些class通过颜色来表示强调.也可以应用于链接,当鼠标盘旋于链接上时,其颜色会变深,就像默认的链接样式. <p class="text-muted"> ...

  8. PHP原始的数据库操作

    <?php    //这是一个工具类;作用是完成对数据库的操作;    class SqlHelper{        public $conn;        public $dbname=& ...

  9. Python学习笔记九-文件读写

    1,读取文件: f=open('目录','读写模式',encoding='gbk,error='egiong') 后三项可以不写但是默认是' r'读模式:open函数打开的文件对象会自动加上read( ...

  10. easy ui 实现gridview效果

    前台: // 加载审批步骤列表 function FillStep(flowID) { $('#tbStepList').datagrid({ url: "/System/ApproverS ...