简体   繁体   English

从NotePad元素中检索不同的值

[英]Retrieve Distinct Values From NotePad Elements

I've a notepad file that has the following format: 我有一个具有以下格式的记事本文件:

at-2017@yahoo.com
at-2017@yahoo.com
at-2018@yahoo.com
at-2018@yahoo.com

I require the following distinct output: 我需要以下不同的输出:

at-2017@yahoo.com
at-2018@yahoo.com

Tried the following code but it doesn't get distinct values: 尝试了以下代码,但它没有得到不同的值:

List<string> lst = new List<string>();
foreach (string line in File.ReadLines(values))
{
   line.Distinct().ToString();
   lst.Add(line);    
}

I know, this may seem stupid and guessing, missed something here. 我知道,这可能看起来很愚蠢和猜测,在这里错过了一些东西。

Distinct() operates on a collection of elements, so you don't need to use it inside the loop. Distinct()对元素集合进行操作,因此您无需在循环内使用它。

Try following: 试试以下:

var lst = File.ReadLines(values).Distinct();
foreach (string line in lst)
{
    Console.WriteLine(line) ;
}

First you should read all the lines and then get the distinct lines: 首先,您应该阅读所有行,然后获得不同的行:

var allLines = File.ReadLines(values);
var distinctLines = allLines.Distinct();
foreach(var distinctLine in distinctLines)
{
    Console.WriteLine(distinctLine);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM