简体   繁体   English

LINQ使用字符串列联接两个表

[英]LINQ joining two tables using string columns

I'm trying to compare two tables. 我正在尝试比较两个表。 I'd like to find out which rows exist in table1 , and don't exist in table2 . 我想找出table1中存在哪些行,而table2中不存在。 oData and oDataiSeries are DataTables, and properly populated. oDataoDataiSeries是DataTable,并且已正确填充。

var testing = from table1 in oData.AsEnumerable()
                          join table2 in oDataiSeries.AsEnumerable() on table1.Field<string>("SLOT") equals table2.Field<string>("SLOT")
                          where table1.Field<string>("SLOT") != table2.Field<string>("SLOT")
                          select table1;

testing ends up being null after the code runs. 代码运行后, 测试最终为null。 But if I were to do the following. 但是,如果我要执行以下操作。

var testingtable1 = from table1 in oData.AsEnumerable()
                                select table1;

            var testingtable2 = from table2 in oDataiSeries.AsEnumerable()
                                select table2;

testingtable1 and testingtable2 will be populated. testingtable1testingtable2将被填充。

Is it because I'm trying to compare strings? 是因为我要比较字符串吗?

in testingtable1 and testingtable2 , the column "SLOT" is String testingtable1testingtable2中 ,列“ SLOT”为字符串

Part of the problem: I'm using VS 2015, and while debugging (hover mouse over DataTable object that should contain results), the Table isn't showing the proper results. 问题的一部分:我正在使用VS 2015,并且在调试时(将鼠标悬停在应包含结果的DataTable对象上),该表未显示正确的结果。 It's showing the value of oData, instead of the comparison between oData and oDataiSeries. 它显示的是oData的值,而不是oData和oDataiSeries之间的比较。 I'm not sure if I'm just misunderstanding how VS2015 displays var tables, or if this is a bug. 我不确定是否只是误解了VS2015如何显示var表,或者这是一个错误。 If I bind the results DataTable to a gridview object, it'll display different rows as compared to the debugger. 如果将结果DataTable绑定到gridview对象,则与调试器相比,它将显示不同的行。 I ended up using @D Stanley solution, with a bit of modification. 我最终使用了@D Stanley解决方案,并做了一些修改。

I'd like to find out which rows exist in table1, and don't exist in table2. 我想找出table1中存在哪些行,而table2中不存在。

You don't need a join for that. 您不需要加入。 Just look for records that don't have a match in the second table: 只需在第二张表中查找不匹配的记录:

var orphans = oData.AsEnumerable()
                   .Where(d => !oDataiSeries.AsEnumerable()
                                            .Any(s => s.Field<string>("SLOT").Trim() == d.Field<string>("SLOT").Trim()));

or if you want to extract the values first to possibly improve performance, try 或者如果您想先提取值以提高性能,请尝试

var series = oDataiSeries.AsEmumerable()
                         .Select(s => s.Field<string>("SLOT"));
var orphans = oData.AsEmumerable()
                   .Where(d => !oDataiSeries.Contains(d.Field<string>("SLOT"));

Is it because I'm trying to compare strings? 是因为我要比较字符串吗?

No, it's because join is an inner join by default, so ALL records will have matching values between the two datasets. 不,这是因为默认情况下join是一个内部联接 ,所以所有记录在两个数据集之间将具有匹配的值。

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

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