2016-12-27 119 views
0

我要得到相同的结果,使用此代码删除为文本文档重复了:加载到列表或上舞台后如何删除从列表C#重复

File.WriteAllLines(@"doc.txt", lines.Select(line1 => line1.Trim()).Distinct().ToArray()); 

同为大内容列表加载但不能从处理的文件:

List<string> contentList = new List<string>(); 

阅读内容我使用这种方式:

for (int i = 0; i < contentList.Count; i++) 
{       
    textBox8.AppendText(contentList[i] + "\n");       
} 

和装载阶段也是循环字符串中的字符串:

contentList.Add(inputStr); 

期望的结果是避免重复,如果我走进去重复:

one 
two 
three 
four 
two 
five 
six 
three 

理想的结果应该是:

one 
two 
three 
four  
five 
six 

回答

3

如果你有字符串列表,你可以使用LINQ的Distinct()方法。

var result = contentList.Distinct(); 

如果您有对象列表,那么您将需要实现自定义相等比较器,这将允许您应用自定义不同规则。

请确保您包含System.Linq命名空间。

+0

确实非常有用 – nikorio