2017-10-09 64 views
0

我有兩個數據表說cSVData和SQLDATA我想比較這兩個數據表的記錄是相同的或不那麼我使用這個查詢動態LINQ查詢的數據表

 var matched = from table1 in cSVData.AsEnumerable() 
         join table2 in sqlData.AsEnumerable() on 
         table1.Field<string>("GlobalRank") equals table2.Field<string>("GlobalRank") 
         where 
         table1.Field<string>("GlobalRank") == table2.Field<string>("GlobalRank") 
         || table1.Field<string>("TldRank") == table2.Field<string>("TldRank") 
         || table1.Field<string>("Domain") == table2.Field<string>("Domain") 
         || table1.Field<string>("TLD") == table2.Field<string>("TLD") 
         || table1.Field<string>("RefSubNets") == table2.Field<string>("RefSubNets") 
         || table1.Field<string>("RefIPs") == table2.Field<string>("RefIPs") 
         || table1.Field<string>("IDN_Domain") == table2.Field<string>("IDN_Domain") 
         || table1.Field<string>("IDN_TLD") == table2.Field<string>("IDN_TLD") 
         || table1.Field<string>("PrevGlobalRank") == table2.Field<string>("PrevGlobalRank") 
         || table1.Field<string>("PrevTldRank") == table2.Field<string>("PrevTldRank") 
         || table1.Field<string>("PrevRefSubNets") == table2.Field<string>("PrevRefSubNets") 
         select table1; 

但這個表中的列名必須在where子句中應該採取所有列名

+0

如果您在'「GlobalRank」'上測試,因爲您正在使用連接,您將獲得每個連接行,而其餘條件無關緊要。 – NetMage

回答

1

假設你cSVDatasqlDataDataTables,你應該能夠使用lambda語法通過列步驟動態方式:

var match = from table1 in cSVData.AsEnumerable() 
      join table2 in sqlData.AsEnumerable() on 
      table1.Field<string>("GlobalRank") equals table2.Field<string>("GlobalRank") 
      select new { table1, table2 }; 
foreach (var colname in cSVData.Columns.Cast<DataColumn>().Select(c => c.ColumnName)) 
    if (colname != "GlobalRank") 
     match = match.Where(both => both.table1.Field<string>(colname) == both.table2.Field<string>(colname)); 

match = match.Select(both => both.table1);