2014-10-19 86 views
0

劇情簡介: -
兩個數據表,第一列有文件名列表。 使用數據表A中的文件名「搜索」數據表B &用結果更新第三個數據表。使用If語句掙扎與foundRows部分一起工作。if statement with datatables

任何提示,我如何得到這個功能?

 // Iterate through the leftFileDT, extract the filename & search rightFileDT 
     for (int i = 0; i < leftFileDT.Rows.Count - 1; i++) 
     { 
      // Extract the leftFileName & store it in a string variable 
      leftFileMatch = leftFileDT.Rows[i][0].ToString(); 

      // Search the rightFileDT for the leftFileMatch string 
      string expression; 
      expression = "Right_File_Name = '" + leftFileMatch + "'"; 
      DataRow[] foundRows; 
      foundRows = rightFileDT.Select(expression); 

      // If no match is found 
      if (notfound) 
      { 
       matchedFileDataRow["File_Match"] = "False"; 
       matchedFileDataRow["Left_File_Name"] = leftFileMatch; 
       matchedFileDT.Rows.Add(matchedFileDataRow); 
      } 
      // If a match is found 
      if (found) 
      { 
       // Update the matchedFileDT datatable 
       matchedFileDataRow["File_Match"] = "True"; 
       matchedFileDataRow["Left_File_Name"] = leftFileMatch; 
       matchedFileDT.Rows.Add(matchedFileDataRow); 

      } 



     // Report progress to 'UI' thread 
     comparerWorker_Left.ReportProgress(i); 
+0

在數組上調用'ToString()'不會幫助你。取而代之的是使用像'bool found = foundRows.Count> 0'這樣的東西。 – Michael 2014-10-19 19:41:41

+0

與論壇網站不同,我們不使用「謝謝」或「任何幫助表示讚賞」,或在[so]上簽名。請參閱[應該'嗨','謝謝,'標語和致敬從帖子中刪除?](http://meta.stackoverflow.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-刪除 - 從 - 個)。 – rene 2014-10-19 19:42:15

回答

0

datatable的Select方法返回一個匹配過濾條件的行數組。
如果沒有行匹配,則數組爲空。

DataRow[] foundRows = rightFileDT.Select(expression); 
if (foundRows.Length == 0) 
{ 
    matchedFileDataRow["File_Match"] = "False"; 
} 
else 
{ 
    matchedFileDataRow["File_Match"] = "True"; 
} 
matchedFileDataRow["Left_File_Name"] = leftFileMatch; 
matchedFileDT.Rows.Add(matchedFileDataRow);