2017-10-09 2 views
0

I ont deux tables de données disent CSVData et SQLData je veux comparer ce disque à deux tables de données est-il même ou pas si j'utilise cette requêtedynamique requête LINQ sur la table de données

 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; 

Mais ce nom de la table des colonnes doivent être des moyens dynamiques où la clause devrait prendre tout nom de colonne

+0

Si vous testez sur "" GlobalRank "' puisque vous utilisez une jointure, vous obtiendrez toutes les lignes de jointure et le reste des conditions n'aura aucune importance. – NetMage

Répondre

1

en supposant que votre cSVData et sqlData sources sont DataTables, vous devriez être en mesure de parcourir les colonnes en utilisant la syntaxe 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);