繁体   English   中英

左外部联接以使用Linq执行DataTable比较

[英]Left outer join to perform DataTable Comparison using Linq

好吧,我正在尝试使用LINQ语句比较两个DataTables

我的桌子的结构是这样的

EmpId -- FirstName -- LastName -- DOB

Emp是唯一键(当然是整数),DOB是Date,其余是字符串。

有两个数据表

SourceTable和TargetTable

TargetTable的记录数可能相同或更少。 FirstName,LastName或DOB的某些值可能不同。

这是没有左外部联接的查询,但没有获取其余的Source记录。

    var matched = from dtSource in sourceTable.AsEnumerable()
                  join dtTarget in targetTable.AsEnumerable() on dtSource.Field<int>("empid") equals dtTarget.Field<int>("empid")

                  where dtSource.Field<string>("firstname") != dtTarget.Field<string>("firstname")
                  || dtSource.Field<string>("lastname") != dtTarget.Field<string>("lastname")
                  || dtSource.Field<DateTime>("dob") != dtTarget.Field<DateTime>("dob")
                  select dtSource;

我正在尝试进行左外部联接,以便获得目标数据表中不存在的其余记录。

这就是我正在尝试的

    var join = from dtSource in source
               join dtTarget in target
                   on dtSource.Field<int>("EmpId") equals dtTarget.Field<int>("EmpId")
               into outer
               where
               dtSource.Field<string>("firstname") != dtTarget.Field<string>("firstname")
                  || dtSource.Field<string>("lastname") != dtTarget.Field<string>("lastname")
                  || dtSource.Field<DateTime>("dob") != dtTarget.Field<DateTime>("dob")

               from dtEmpSal in outer.DefaultIfEmpty()

               select new
               {
                   FirstName = dtSource.Field<string>("FirstName") == null ? "" : dtSource.Field<string>("FirstName"),
                   LastName = dtSource.Field<string>("LastName") == null ? "" : dtSource.Field<string>("LastName"),
                   DOB = dtSource.Field<DateTime>("dob") == null ? DateTime.Today : dtSource.Field<DateTime>("dob")
               };

当然,我做错了,因为我无法通过where子句中的dtTarget进行编译。 我做错了事,无法弄清楚是什么。

编辑:

这就是我的源数据表的样子

资源

这就是我的目标的样子

目标

在我的情况下,结果集应包括带有empIds 1,6,7的emp记录,依此类推。 注意1是不同的(姓氏),并且目标DataTable中包含2、3、4、5。

截屏显示我的麻烦:

https://www.dropbox.com/s/145ba6oa59fstdc/screencap.wmv

我发现很难调试它:(

这就是帮助我的原因

public static dynamic GetAllDifferences (DataTable sourceTable,DataTable targetTable)
        {
            var source = sourceTable.AsEnumerable();
            var target = targetTable.AsEnumerable();

            var noMatchInSource = from dtSource in source
                join dtTarget in target on dtSource.Field<int>("empid") equals dtTarget.Field<int>("empid")
                where dtSource.Field<string>("firstname").Equals(dtTarget.Field<string>("firstname"))
                      && dtSource.Field<string>("lastname").Equals(dtTarget.Field<string>("lastname"))
                      && dtSource.Field<DateTime>("dob").Equals(dtTarget.Field<DateTime>("dob"))
                select dtSource;

            var result =
                        from sourceDataRow in sourceTable.AsEnumerable()
                        where !(from noMatchDataRow in noMatchInSource
                                select noMatchDataRow.Field<int>("empid"))
                               .Contains(sourceDataRow.Field<int>("empid"))
                        select sourceDataRow;
                                return result;
        }

暂无
暂无

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

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