如何从列表中删除重复项< listviewitem>没有改变它的类型?
我有一个List< listviewitem>我将稍后使用.AddRange方法添加到ListView中。该列表包含重复项,因为条目来自不同的来源。
如何从列表< listviewitem>中删除重复项?在不改变元素顺序的情况下改变它的类型,
,使用普通C#代码(没有外部库)并且没有额外的编码循环来重新使用
填充,删除和检查?
我试过这个对我有用,但之后整个类型都丢失了:
列表< listviewitem> MyList = new List< listviewitem>();
//...我的List正在填充并包含重复项...(不包含代码)
List<串GT; tempvar = MyList.Select(x => x.Name).Distinct()。ToList();
List< string> tempvar基本上包含了我需要的内容,但我需要它作为List< listviewitem>不是List< string>。我是一个初学者,我试过的所有其他东西对我来说都不像Hashing和其他的建议。
后来我执行添加整个列表到listview如下:
MylistView.AddRange(MyList.toArray());
在此调用之后我不再检查重复项,代码必须在它之前。
我尝试了什么:
对linq和distinct的不同建议,到目前为止没有任何工作。
I have a List<listviewitem> which I will add later to my ListView with the .AddRange Method. The list contains duplicates because the entries are from different sources.
How do I remove duplicates from a List<listviewitem> without changing its type,
without changing the order of the elements,
with the usage of plain C# code (no external libraries) and without an extra coded loop for re-populating, deleting and checking?
I tried this which works for me, but the whole type is lost afterwards:
List<listviewitem> MyList = new List<listviewitem>();
//...here my List is being populated and contains duplicates... (Code not included)
List<string> tempvar = MyList.Select(x => x.Name).Distinct().ToList();
List<string> tempvar contains basically what I would need, but I need it as a List<listviewitem> not a List<string>. I am a beginner and all other stuff which I tried did not work for me like the Hashing and other sugggestions.
Later I perform the adding of the whole list to the listview as follows:
MylistView.AddRange(MyList.toArray());
After this call I would not check for duplicates anymore, the code must go before it.
What I have tried:
different suggestions for linq and distinct, nothing worked so far.
我的第一个建议,如果可能,将首先收集不同的数据列表然后从在那里为每个创建一个ListViewItem
。
另一种方式:
尝试使用.Distinct()
的版本,它带有IEqualityComparer< TSource>
参数。
例如:
My first suggestion, if possible, would be to collect the list of distinct data first and then go from there to create aListViewItem
for each.
Another way:
Try using the version of.Distinct()
that takes anIEqualityComparer<TSource>
parameter.
Like:
class LviNameComparer : IEqualityComparer<ListViewItem>
{
// implement Equals and GetHashCode methods of the interface
public bool Equals(ListViewItem lvi1, ListViewItem lvi2)
{
return ReferenceEquals(lvi1, lvi2) ||
(lvi1 != null &&
lvi2 != null &&
lvi1.Name.Equals(lvi2.Name));
}
public int GetHashCode(ListViewItem lvi)
{
return lvi == null ?
0 :
(lvi.Name ?? string.Empty).GetHashCode();
}
}
List<string> tempvar = MyList.Distinct(new LviNameComparer()).ToList();
你'我基本上都在寻找一个.DistinctBy(..)方法,它不包含在标准.NET中,但有一些实现,其中一个可以在这里找到:
GitHub - morelinq / MoreLINQ:LINQ to Objects扩展 [ ^ ]
..允许你这样做:
You're basically looking for a .DistinctBy(..) method, which isn't included in standard .NET but there are implementations, one of which you can find here:
GitHub - morelinq/MoreLINQ: Extensions to LINQ to Objects[^]
..which will allow you to do this:
List<listviewitem> distinctItems = MyList.DistinctBy(x => x.Name).ToList();
编辑:
如果您不想将整个MoreLINQ项目包含在您的解决方案中,请使用以下代码并将其复制到其中拥有C#-source-file:
If you don't want to include the whole MoreLINQ-project into your solution, use just the following code and copy it into its own C#-source-file:
using System;
using System.Collections.Generic;
namespace LinqExtensions
{
public static class LinqExtensionDistinctBy
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> keys = new HashSet<TKey>();
foreach (TSource item in source)
{
if (keys.Add(keySelector(item)))
{
yield return item;
}
}
}
}
}
使用LinqExtensions添加; -directive您要使用此扩展方法的文件。
Add a using LinqExtensions; -directive to your files where you want to use this extension method.