2011-08-31 195 views
0

可能重複:
Remove duplicates from a List<T> in C#如何刪除沒有LINQ的列表<string>中的重複項?

我有一個像下方的列表(這麼大的郵件列表):
源列表:

item 0 : [email protected]|32432 
item 1 : [email protected]|32432|test23 
item 2 : [email protected]|32432|test65 
item 3 : [email protected]|32432|test32 

各的重要組成部分項目是電子郵件地址,其他部分(用管道分開並不重要),但我想保留它們在最終名單中。
正如我所說我的名單是大,我認爲不建議使用另一個名單。

我怎樣才能刪除重複的電子郵件(整個項目)形式的列表而不使用LINQ?
我的代碼如下圖所示:

private void WorkOnFile(UploadedFile file, string filePath) 
{ 
    File.SetAttributes(filePath, FileAttributes.Archive); 

    FileSecurity fSecurity = File.GetAccessControl(filePath); 
    fSecurity.AddAccessRule(new FileSystemAccessRule(@"Everyone", 
                FileSystemRights.FullControl, 
                AccessControlType.Allow)); 
    File.SetAccessControl(filePath, fSecurity); 

    string[] lines = File.ReadAllLines(filePath); 
    List<string> list_lines = new List<string>(lines); 
    var new_lines = list_lines.Select(line => string.Join("|", line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries))); 
    List<string> new_list_lines = new List<string>(new_lines); 
    int Duplicate_Count = 0; 
    RemoveDuplicates(ref new_list_lines, ref Duplicate_Count); 
    File.WriteAllLines(filePath, new_list_lines.ToArray()); 
} 

private void RemoveDuplicates(ref List<string> list_lines, ref int Duplicate_Count) 
{ 
    char[] splitter = { '|' }; 
    list_lines.ForEach(delegate(string line) 
    { 
     // ?? 
    }); 
} 

編輯:
在該列表中一些重複的電子郵件addrresses有不同的部分 - >
我能做些什麼關於他們:
意味着

[email protected]|32432|test23 
and 
[email protected]|asdsa|324234 

在此先感謝。

+0

不重複 - 我的q是不同的/ PLZ看到我的評論... – MoonLight

+1

爲什麼神的名字,你有「沒有LINQ '作爲一項要求? – Steven

+0

@Steven尋找和學習可能的方式...... – MoonLight

回答

1
private void RemoveDuplicates(ref List<string> list_lines, ref int Duplicate_Count) 
{ 
    Duplicate_Count = 0; 
    List<string> list_lines2 = new List<string>(); 
    HashSet<string> hash = new HashSet<string>(); 

    foreach (string line in list_lines) 
    { 
     string[] split = line.Split('|'); 
     string firstPart = split.Length > 0 ? split[0] : string.Empty; 

     if (hash.Add(firstPart)) 
     { 
      list_lines2.Add(line); 
     } 
     else 
     { 
      Duplicate_Count++; 
     } 
    } 

    list_lines = list_lines2; 
} 
1

說,你有可能重複的名單:

List<string> emailList .... 

那麼唯一列表是一組名單:

HashSet<string> unique = new HashSet<string>(emailList) 
+0

感謝您的回答/但在該列表中的一些重複的電子郵件地址有不同的部分 - >我能做些什麼關於他們(意思是:[email protected]|32432|test23和[email protected]|asdsa|324234) – MoonLight

0

做最簡單的事情是通過行迭代該文件並將它們添加到HashSet。 HashSets不會插入重複條目,也不會生成異常。最後,您將擁有一個唯一的項目列表,並且不會爲任何重複項目生成例外。

0

1 - 擺脫你管分離字符串(創建對應的數據它代表的DTO類)

2 - 你要應用到選擇具有相同id兩個Object哪個規則?

0

或許這些代碼可能對您有用:) 它使用同樣的方法,將一個在@xanatos回答

string[] lines= File.ReadAllLines(filePath); 
Dictionary<string, string> items; 

foreach (var line in lines) 
{ 
    var key = line.Split('|').ElementAt(0); 
    if (!items.ContainsKey(key)) 
     items.Add(key, line); 
} 
List<string> list_lines = items.Values.ToList(); 
0

首先,我建議你通過流加載該文件。 然後,創建一個表示行的類型並將它們加載到HashSet中(對於 性能注意事項)。

看(IVE刪除了一些代碼,使其簡單):

public struct LineType 
{ 
    public string Email { get; set; } 
    public string Others { get; set; } 

    public override bool Equals(object obj) 
    { 
     return this.Email.Equals(((LineType)obj).Email); 
    } 
} 
private static void WorkOnFile(string filePath) 
{ 
    StreamReader stream = File.OpenText(filePath); 

    HashSet<LineType> hashSet = new HashSet<LineType>(); 

    while (true) 
    { 
     string line = stream.ReadLine(); 
     if (line == null) 
      break; 

     string new_line = string.Join("|", line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)); 


     LineType lineType = new LineType() 
     { 
      Email = new_line.Split('|')[3], 
      Others = new_line 
     }; 

     if (!hashSet.Contains(lineType)) 
      hashSet.Add(lineType); 
    } 
}