DataTables动态联接 [英] DataTables Dynamic Join

查看:99
本文介绍了DataTables动态联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DataTables上搜索了很多有关动态联接的内容, 我发现:

使用动态列的内部联接

数据表上的内部联接

动态Linq进行动态联接

加入数据表支持左右全

但是我找不到任何一般的解决方案,假设这种情况下要联接两个表:

DT1 Left Join DT2 On DT1.Id != DT2.Id

DT1 Right Join DT2 On ((DT1.Age + 2) = DT2.Age) OR (DT1.BirthDate > DT2.BirthDate))

如您所见,我需要一些通用的解决方案来在两个具有复杂Where子句的数据表之间动态联接,我有一个数据结构来支持复杂的Where子句,但是如何动态联接呢?有什么建议吗?

解决方案

如果在3.0或更高版本上运行,则可以在此处使用LINQ.我知道这是不正确的,只是给您一个想法.您需要在此代码中进行一些搜索.只是寻求帮助.如果未找到匹配的记录,则已将DefaultIfEmpty应用于要从其返回null的表.

var dt = (from dt1  in DT1.AsEnumerable()
          from dt2 in DT2.AsEnumerable().Where(dt1.Id==dt2.Id).DefaultIfEmpty()
          select new {
           dt1.Column
          }).CopyToDataTable();

I search a lot about dynamic join on DataTables, I found:

Inner Join With Dynamic Columns

Inner Join On DataTables

Dynamic Linq To Dynamic Join

Join DataTable Support Left-Right-Full

But I can't find any general solution, assume this scenario for join two tables:

DT1 Left Join DT2 On DT1.Id != DT2.Id

Or

DT1 Right Join DT2 On ((DT1.Age + 2) = DT2.Age) OR (DT1.BirthDate > DT2.BirthDate))

As you see I need some general solution to Dynamically Join between two DataTables with complex Where clause, I have a Data Structure to support complex Where Clauses, But How can I Join Dynamically? any suggestion?

解决方案

You can use LINQ here, if working on 3.0 or above. I know its not correct but just to give you an idea. You need to do some reasearch in this code. Just for help. You have apply DefaultIfEmpty to the table from which you want to return null in case no matching records found.

var dt = (from dt1  in DT1.AsEnumerable()
          from dt2 in DT2.AsEnumerable().Where(dt1.Id==dt2.Id).DefaultIfEmpty()
          select new {
           dt1.Column
          }).CopyToDataTable();

这篇关于DataTables动态联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆