动态LINQ的加入对数据表动态列 [英] Dynamic LINQ to join on dynamic columns on DataTable

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

问题描述

我有我目前不能确定如何进行的情况。

I have a situation that I am currently unsure of how to proceed.

我有一个从数据库填充两个DataTable,我也有列的列表可用名称可以使用这些两个DataTable连接在一起。我希望写一组LINQ查询将:

I have two datatables that are populated from a database, I also have a list of column names available that can be used to join these two datatables together. I wish to write a set of LINQ queries that will:


  1. 从两个数据表显示行(内部连接,用于更新从另一个)

  2. 从没有在其他中存在一个数据表显示行(一个查询,左用于插入,另一个是右连接用于删除连接)。

现在我知道如何与普通的LINQ to对象或数据表做到这一点,但是在这种情况下,我需要申请的列动态加入,并有可以是一个以上。看着下面的部分示例代码:

Now I know how to do this with normal LINQ to objects or datatables, however in this case I need to apply the columns to join on dynamically, and there could be more than one. Looking at the following partial example code:

table1.AsEnumerable()
.Join(table2.AsEnumerable(),
    dr1 => dr1.Field<string>("ID"),
    dr2 => dr2.Field<string>("ID"),
    (dr1, dr2) => new
    {
        FieldID = dr1.Field<string>("ID"),
        CdGroup = dr2.Field<string>("Name")
    })

的问题是,我不知道字段类型,这样技术领域< ;串GT; 语句的部分不能被应用。此外,如果他们有多个连接列,那么我就需要有多个连接语句。

The issues are that I don't know the field type so the .Field<string> parts of the statement can't be applied. Also if their are multiple join columns, then I will need to have multiple join statements.

我在动态LINQ读了,它看起来相当有前途的,但是我的天堂 ŧ设法找到动态LINQ的任何信息加入像我想要做的事。我知道我可以使用嵌套循环或在DataTable中的。选择()方法相同的结果可能会得到,但我想LINQ应用到一些强硬的查询是我要求。

I have read up on dynamic LINQ and it seems quite promising, however I haven't managed to find any information on dynamic LINQ joins like I am trying to do. I know I could probably get the same results using nested loops or the.Select() method on the datatable, but I am trying to apply LINQ to some of the tougher queries that I require.

不要任何人有我如何能做到这一点的任何指针或例子,或者我应该只是恢复到使用非LINQ的方法呢?

Do anyone have any pointers or examples of how I could achieve this, or should I just revert to using a non-LINQ approach?

非常感谢。

推荐答案

如果您使用的是实体框架,你可以下载Microsoft Dynamics CRM中,并使用模式回答这个问题的是否有办法来构建一个QueryExpression,让你可以动态地处理数目不详的条件

If you are using Entity Framework, you could download the Microsoft Dynamics CRM, and use the pattern answered to this question Is there way to structure a QueryExpression so that you could dynamically handle a unknown number of conditions

有一个构建所谓的QueryExpression从中你可以模拟动态查询。看到这个MSDN文章 http://msdn.microsoft .COM / EN-US /库/ microsoft.xrm.sdk.query.queryexpression.aspx

There is a construct called QueryExpression from which you can model dynamic queries. See this MSDN article http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.query.queryexpression.aspx

这篇关于动态LINQ的加入对数据表动态列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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