Fac*_*tic 781
如果您使用的是.Net 3+,则可以使用Linq.
List<T> withDupes = LoadSomeData();
List<T> noDupes = withDupes.Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
Jas*_*ker 215
也许您应该考虑使用HashSet.
从MSDN链接:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
HashSet<int> evenNumbers = new HashSet<int>();
HashSet<int> oddNumbers = new HashSet<int>();
for (int i = 0; i < 5; i++)
{
// Populate numbers with just even numbers.
evenNumbers.Add(i * 2);
// Populate oddNumbers with just odd numbers.
oddNumbers.Add((i * 2) + 1);
}
Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
DisplaySet(evenNumbers);
Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count);
DisplaySet(oddNumbers);
// Create a new HashSet populated with even numbers.
HashSet<int> numbers = new HashSet<int>(evenNumbers);
Console.WriteLine("numbers UnionWith oddNumbers...");
numbers.UnionWith(oddNumbers);
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
}
private static void DisplaySet(HashSet<int> set)
{
Console.Write("{");
foreach (int i in set)
{
Console.Write(" {0}", i);
}
Console.WriteLine(" }");
}
}
/* This example produces output similar to the following:
* evenNumbers contains 5 elements: { 0 2 4 6 8 }
* oddNumbers contains 5 elements: { 1 3 5 7 9 }
* numbers UnionWith oddNumbers...
* numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }
*/
Run Code Online (Sandbox Code Playgroud)
ljs*_*ljs 160
怎么样:-
var noDupes = list.Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
在.net 3.5?
Eve*_*ien 87
只需使用相同类型的List初始化HashSet:
var noDupes = new HashSet<T>(withDupes);
Run Code Online (Sandbox Code Playgroud)
或者,如果您想要返回List:
var noDupsList = new HashSet<T>(withDupes).ToList();
Run Code Online (Sandbox Code Playgroud)
ang*_*son 45
对它进行排序,然后检查彼此旁边的两个和两个,因为重复项将聚集在一起.
像这样的东西:
list.Sort();
Int32 index = list.Count - 1;
while (index > 0)
{
if (list[index] == list[index - 1])
{
if (index < list.Count - 1)
(list[index], list[list.Count - 1]) = (list[list.Count - 1], list[index]);
list.RemoveAt(list.Count - 1);
index--;
}
else
index--;
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*ric 30
我喜欢用这个命令:
List<Store> myStoreList = Service.GetStoreListbyProvince(provinceId)
.GroupBy(s => s.City)
.Select(grp => grp.FirstOrDefault())
.OrderBy(s => s.City)
.ToList();
Run Code Online (Sandbox Code Playgroud)
我在列表中有这些字段:Id,StoreName,City,PostalCode我想在下拉列表中显示具有重复值的城市列表.解决方案:逐个城市然后选择第一个列表.
我希望它有帮助:)
Hos*_*har 30
它对我有用.简单地用
List<Type> liIDs = liIDs.Distinct().ToList<Type>();
Run Code Online (Sandbox Code Playgroud)
将"类型"替换为您想要的类型,例如int.
Kei*_*ith 22
正如kronoz在.Net 3.5中所说,你可以使用Distinct().
在.Net 2中你可以模仿它:
public IEnumerable<T> DedupCollection<T> (IEnumerable<T> input)
{
var passedValues = new HashSet<T>();
// Relatively simple dupe check alg used as example
foreach(T item in input)
if(passedValues.Add(item)) // True if item is new
yield return item;
}
Run Code Online (Sandbox Code Playgroud)
这可用于重复数据删除任何集合,并将按原始顺序返回值.
过滤一个集合(就像Distinct()这个和这个样本一样)通常比从中删除项目要快得多.
小智 12
扩展方法可能是一个不错的方式...这样的事情:
public static List<T> Deduplicate<T>(this List<T> listToDeduplicate)
{
return listToDeduplicate.Distinct().ToList();
}
Run Code Online (Sandbox Code Playgroud)
然后像这样打电话,例如:
List<int> myFilteredList = unfilteredList.Deduplicate();
Run Code Online (Sandbox Code Playgroud)
Tom*_*ine 11
在Java中(我假设C#或多或少相同):
list = new ArrayList<T>(new HashSet<T>(list))
Run Code Online (Sandbox Code Playgroud)
如果你真的想改变原始列表:
List<T> noDupes = new ArrayList<T>(new HashSet<T>(list));
list.clear();
list.addAll(noDupes);
Run Code Online (Sandbox Code Playgroud)
要保留顺序,只需使用LinkedHashSet替换HashSet即可.
小智 9
这需要不同的元素(没有重复的元素),然后将其再次转换为列表:
List<type> myNoneDuplicateValue = listValueWithDuplicate.Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
通过 Nuget安装MoreLINQ包,您可以通过属性轻松区分对象列表
IEnumerable<Catalogue> distinctCatalogues = catalogues.DistinctBy(c => c.CatalogueCode);
Run Code Online (Sandbox Code Playgroud)
作为辅助方法(没有Linq):
public static List<T> Distinct<T>(this List<T> list)
{
return (new HashSet<T>(list)).ToList();
}
Run Code Online (Sandbox Code Playgroud)
使用Linq的Union方法.
注意:除了存在之外,该解决方案不需要Linq的知识.
码
首先将以下内容添加到类文件的顶部:
using System.Linq;
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用以下命令从名为的对象中删除重复项obj1:
obj1 = obj1.Union(obj1).ToList();
Run Code Online (Sandbox Code Playgroud)
注意:重命名obj1为对象的名称.
这个怎么运作
Union命令列出两个源对象的每个条目之一.由于obj1都是源对象,因此将obj1减少为每个条目之一.
在ToList()返回一个新的列表.这是必要的,因为Linq命令Union会将结果作为IEnumerable结果返回,而不是修改原始List或返回新List.
如果你不关心顺序你可以推的项目进入HashSet,如果你不想要保持你可以做这样的事情的顺序:
var unique = new List<T>();
var hs = new HashSet<T>();
foreach (T t in list)
if (hs.Add(t))
unique.Add(t);
Run Code Online (Sandbox Code Playgroud)
或者Linq方式:
var hs = new HashSet<T>();
list.All( x => hs.Add(x) );
Run Code Online (Sandbox Code Playgroud)
编辑:该HashSet方法是O(N)时间和O(N)空间,同时排序,然后作出独特的(如@建议lassevk等)是O(N*lgN)时间和O(1)空间,所以它不是那么清楚,我(因为它是在第一眼)的排序方式是劣质(我为临时投票表示道歉...)
这是一种用于原位移除相邻重复项的扩展方法.首先调用Sort()并传入相同的IComparer.这应该比Lasse V. Karlsen的版本更有效,它反复调用RemoveAt(导致多个块内存移动).
public static void RemoveAdjacentDuplicates<T>(this List<T> List, IComparer<T> Comparer)
{
int NumUnique = 0;
for (int i = 0; i < List.Count; i++)
if ((i == 0) || (Comparer.Compare(List[NumUnique - 1], List[i]) != 0))
List[NumUnique++] = List[i];
List.RemoveRange(NumUnique, List.Count - NumUnique);
}
Run Code Online (Sandbox Code Playgroud)
如果您有两个班级Product并且Customer我们想从他们的列表中删除重复的项目
public class Product
{
public int Id { get; set; }
public string ProductName { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string CustomerName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
您必须以下面的形式定义一个泛型类
public class ItemEqualityComparer<T> : IEqualityComparer<T> where T : class
{
private readonly PropertyInfo _propertyInfo;
public ItemEqualityComparer(string keyItem)
{
_propertyInfo = typeof(T).GetProperty(keyItem, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
}
public bool Equals(T x, T y)
{
var xValue = _propertyInfo?.GetValue(x, null);
var yValue = _propertyInfo?.GetValue(y, null);
return xValue != null && yValue != null && xValue.Equals(yValue);
}
public int GetHashCode(T obj)
{
var propertyValue = _propertyInfo.GetValue(obj, null);
return propertyValue == null ? 0 : propertyValue.GetHashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以删除列表中的重复项。
var products = new List<Product>
{
new Product{ProductName = "product 1" ,Id = 1,},
new Product{ProductName = "product 2" ,Id = 2,},
new Product{ProductName = "product 2" ,Id = 4,},
new Product{ProductName = "product 2" ,Id = 4,},
};
var productList = products.Distinct(new ItemEqualityComparer<Product>(nameof(Product.Id))).ToList();
var customers = new List<Customer>
{
new Customer{CustomerName = "Customer 1" ,Id = 5,},
new Customer{CustomerName = "Customer 2" ,Id = 5,},
new Customer{CustomerName = "Customer 2" ,Id = 5,},
new Customer{CustomerName = "Customer 2" ,Id = 5,},
};
var customerList = customers.Distinct(new ItemEqualityComparer<Customer>(nameof(Customer.Id))).ToList();
Run Code Online (Sandbox Code Playgroud)
此代码删除重复项,Id如果您想通过其他属性删除重复项,您可以更改nameof(YourClass.DuplicateProperty) 相同内容,nameof(Customer.CustomerName)然后按CustomerName属性删除重复项。