LEFT JOIN DataTables-如何使它工作? [英] LEFT JOIN DataTables - how do I get this to work?

查看:114
本文介绍了LEFT JOIN DataTables-如何使它工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
左外联接2个数据表

Possible Duplicate:
LEFT OUTER JOIN 2 datatables

让我们说这是我的确切代码:

Let's say this is my exact code:

        DataTable table1 = new DataTable();
        DataTable table2 = new DataTable();
        DataTable joinedtable = new DataTable();

        table1.Columns.Add("ID");
        table1.Columns.Add("ColA");
        table1.Rows.Add(1, "A");

        table2.Columns.Add("ID");
        table2.Columns.Add("ColB");
        table2.Rows.Add(1, "B");

我的查询(无论是否为LINQ)会如何处理,但是将table1和table2合并在一起并放入joinedTable中呢? 当我尝试搜索示例时,会不断查找以以下形式开头的示例:

What would my query, LINQ or not, but to left join table1 and table2 together and put it into joinedTable? When I try to search for examples, I keep finding examples that start like this:

        var result = from x in table1
            join y in table2 on x.ID equals y.ID 

但是,我在DataTable1上遇到错误,我发现建议改用"table1.AsEnumerable()",但随后我没有x.ID.未定义.ID.我只有普通DataRow会拥有的x东西. 我在这里做错了什么?我似乎无法弄清楚.

But, I get an error on DataTable1 which I found a suggestion to instead do "table1.AsEnumerable()" but then I do not have a x.ID. .ID is not defined. I only have things for x that a normal DataRow would have. What am I doing wrong here? I cannot seem to figure this out.

谢谢大家的帮助.

推荐答案

使用DataTables不幸的是,它变得有些棘手(如果出于任何原因不想使用LINQ),则需要定义 DataRelation 实际上是 的对象定义这两个DataTables之间的关系定义.

With DataTables it unfortunately becomes kind of tricky (if you don't want to use a LINQ for any reason), cause you need to define DataRelation object that actually define the relationship defintion between those two DataTables.

有关更多信息,请参见以下说明:

For more information can have a look on this explanation :

如何加入两个使用C#的数据表

这篇关于LEFT JOIN DataTables-如何使它工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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