2014-10-28 48 views
-1

我有一個元組字典,由三個屬性組成:名稱,地址,電話。如何刪除具有重複屬性的元組中的條目?

例子:

  • 約翰尼·塔爾,1234蓋爾道,555-402-9687
  • 帕特里克·墨菲,1234泰勒路,555-555-5555
  • 帕特里克·墨菲,1234莫里森苑,555 -555-5555

如何刪除三個屬性中的兩個重複的條目?

一個失敗的嘗試遍歷集合:

for (int i = 0; i < fileList.Count - 1; i++) 
{ 
    for (int j = i + 1; j < fileList.Count; j++) 
    { 
     // Test Results: There are supposed to be 362 duplicates. Outputting only 225 entries. A mix of duplicates and not duplicates. 
     if (fileList[i].Item1.Equals(fileList[j].Item1, StringComparison.CurrentCultureIgnoreCase) && fileList[i].Item3.Equals(fileList[j].Item3, StringComparison.CurrentCultureIgnoreCase)) 
     { 
      file.WriteLine(fileList[i].Item1 + "|" + fileList[i].Item2 + "|" + fileList[i].Item3); 
     } 
    } 
} 
+0

請顯示代碼以及您迄今爲止所嘗試的內容。 – 2014-10-28 20:29:31

+0

增加了一些代碼。 – fireundubh 2014-10-28 22:03:54

+0

您需要提供樣本輸入/期望輸出。減少你的「362重複」文件到幾個條目,只是足以重現該問題。此外,這裏的地址並不重要,對(從不復制)?這從你的代碼看起來很清楚,但不是來自問題。 – 2014-10-28 22:33:27

回答

1
var distincts = fileList.GroupBy(t => t.Item1 + "," + t.Item3) 
         .Where(g => g.Count() == 1) 
         .Select(g => g.Single()); 

foreach (var item in distincts) 
{ 
    Console.WriteLine(item); 
} 

這由名稱/電話組的元組,那麼只保留包含單個元組羣,然後選擇,對於單個元組輸出不同元組的列表。

+0

謝謝!這工作完美,從結果中刪除360重複,我用Excel過濾驗證。 (之前的數字362是不準確的。)我也很理解Linq的查詢。我不知道以前的信件是什麼意思。 – fireundubh 2014-10-29 10:41:39