简体   繁体   English

左联接LINQ有麻烦

[英]having trouble with left join LINQ

I have two DataTables t1 and t2 . 我有两个数据表t1t2 I'm trying to perform a LINQ left join, multiple equijoin, to get the DataRows in t1 that are not in t2 . 我正在尝试执行LINQ左联接,即多个等联接,以在t1中获得不在t2的DataRows。

In SQL, what I'm trying to accomplish is: 在SQL中,我要完成的工作是:

select t1.* 
from t1
left join t2
on t1.a=t2.a and
   t1.b=t2.b and
   t1.c=t2.c
where
   t2.a is null

So far I have the following: 到目前为止,我有以下内容:

public DataTable t1_without_t2(DataTable t1, DataTable t2)
        {
            var query = from t1_row in t1.AsEnumerable()
                        join t2_row in t2.AsEnumerable()
                        on 
                        new { t_a = t1_row["a"], t_b = t1_row["b"], t_c = t1_row["c"]}   
                        equals
                        new { t_a = t2_row["a"], t_b = t2_row["b"], t_c = t2_row["c"]}
                        into leftJoinT1withoutT2
                        from join_row in leftJoinT1withoutT2.DefaultIfEmpty()
                        where t2_row["a"] == null
                        select new
                        {
                            j_a = join_row["a"],
                            j_b = join_row["b"],
                            j_c = join_row["c"],
                        };
            DataTable dt = t1.Clone();
            foreach (var result in query)
            {
                dt.LoadDataRow(
                    new object[]
                    {
                        result.j_a,
                        result.j_b,
                        result.j_c
                    },
                    false);
            }
            return dt;
        }

This is failing on the line j_a = join_row["a"] with this message: Column 'a' does not belong to table. 在以下j_a = join_row["a"] ,在行j_a = join_row["a"]上失败: Column 'a' does not belong to table.

I thought that the into leftJoinT1withoutT2 line was supposed to put the results of the join into a var with the column structure of table t1 , from which the non-matching entries would be removed using where t2_row["a"] == null . 我认为, into leftJoinT1withoutT2行应该将连接的结果放入表t1的列结构的var中,使用where t2_row["a"] == null可以从中删除不匹配的条目。 Is that not what's happening here? 那不是这里发生的事吗? I'm a little confused. 我有点困惑。

It should look like this: 它看起来应该像这样:

var query = from t1_row in t1.AsEnumerable()
            join t2_row in t2.AsEnumerable()
            on
            new { t_a = t1_row["a"], t_b = t1_row["b"], t_c = t1_row["c"] }
            equals
            new { t_a = t2_row["a"], t_b = t2_row["b"], t_c = t2_row["c"] }
            into leftJoinT1withoutT2
            from join_row in leftJoinT1withoutT2.DefaultIfEmpty()
                                                .Where(r => r == null)
            select new
            {
                j_a = t1_row["a"],
                j_b = t1_row["b"],
                j_c = t1_row["c"],
            };

Have a look at How to: Perform Left Outer Joins (C# Programming Guide) . 看一看如何:执行左外部联接(C#编程指南)

The join_row gets null (ie default TSource value, see Enumerable.DefaultIfEmpty ) when there is no matching element in t2 , while t1_row always contains the joined value. t2没有匹配元素时, join_row null (即,默认TSource值,请参见Enumerable.DefaultIfEmpty ),而t1_row始终包含联接值。 So as far as you need only those rows for which join_row is null , I used .Where(r => r == null) . 因此,只要您只需要join_rownull那些行,我就使用.Where(r => r == null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM