简体   繁体   English

从列表中获取唯一元素<String>

[英]Get unique elements from a List<String>

I would like to remove the duplicate elements from a List. 我想从列表中删除重复的元素。 Some elements of the list looks like this: 列表中的某些元素如下所示:

Book  23
Book  22
Book  19
Notebook 23
Notebook 22
Notebook 19
Pen 23
Pen 22
Pen 19

I would like to keep in the list just 我只想保留在列表中

Book 23
Notebook 23
Pen 23

How can i do that ? 我怎样才能做到这一点 ?

What about basic looping? 基本循环呢?

List<string> nodup = dup.Distinct().ToList();
List<int> remIndex = new List<int>();
for (int nIdx = 0; nIdx < nodup.Count; nIdx++)
{
    string[] strArr = nodup[nIdx].Split(' ');
    if (String.Compare(strArr[1], "23", true) != 0)
        remIndex.Add(nIdx);
}
foreach (int remIdx in remIndex)
    nodup.RemoveAt(remIdx);

Hope this is of some help... 希望这个对你有帮助...

try this 尝试这个

  List<Person> distinctPeople = allPeople
  .GroupBy(p => p.PersonId)
  .Select(g => g.First())
  .ToList();

from this discussion 从这个讨论

use your column names 使用您的列名

尝试这个

list.Sort();

Int32 index = 0; while (index < list.Count - 1) {
if (list[index] == list[index +1])
list.RemoveAt(index); else index++; }

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

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