使用LINQ,List<int>
如何从中检索包含重复项不止一次的列表及其值?
使用LINQ,List<int>
如何从中检索包含重复项不止一次的列表及其值?
Answers:
解决问题的最简单方法是根据元素的值对其进行分组,如果组中有多个元素,则选择该组的代表。在LINQ中,这转换为:
var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => y.Key)
.ToList();
如果您想知道元素重复了多少次,可以使用:
var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count() })
.ToList();
这将返回List
匿名类型的,并且每个元素都将具有属性Element
和Counter
,以检索所需的信息。
最后,如果您要查找的是字典,则可以使用
var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.ToDictionary(x => x.Key, y => y.Count());
这将返回一个字典,将您的元素作为键,并将其重复的次数作为值。
code
for(int i = 0; i <plicates.Count; i ++)plicatesLocation.Add(重复,新List <int>()); for(int k = 0; k <hitsList.Length; k ++){if(hitsList [k] .Contains(duplicate)){repeatsLocation.ElementAt(i).Value.Add(k); }} //根据某些规则删除重复项。}code
找出可枚举是否包含任何重复项:
var anyDuplicate = enumerable.GroupBy(x => x.Key).Any(g => g.Count() > 1);
找出可枚举中的所有值是否唯一:
var allUnique = enumerable.GroupBy(x => x.Key).All(g => g.Count() == 1);
另一种方法是使用HashSet
:
var hash = new HashSet<int>();
var duplicates = list.Where(i => !hash.Add(i));
如果要在重复项列表中使用唯一值:
var myhash = new HashSet<int>();
var mylist = new List<int>(){1,1,2,2,3,3,3,4,4,4};
var duplicates = mylist.Where(item => !myhash.Add(item)).Distinct().ToList();
这是与通用扩展方法相同的解决方案:
public static class Extensions
{
public static IEnumerable<TSource> GetDuplicates<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IEqualityComparer<TKey> comparer)
{
var hash = new HashSet<TKey>(comparer);
return source.Where(item => !hash.Add(selector(item))).ToList();
}
public static IEnumerable<TSource> GetDuplicates<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
{
return source.GetDuplicates(x => x, comparer);
}
public static IEnumerable<TSource> GetDuplicates<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector)
{
return source.GetDuplicates(selector, null);
}
public static IEnumerable<TSource> GetDuplicates<TSource>(this IEnumerable<TSource> source)
{
return source.GetDuplicates(x => x, null);
}
}
List<int> { 1, 2, 3, 4, 5, 2 }
用作源,结果是IEnumerable<int>
一个元素的值为1
(其中正确的重复值为2)
Console.WriteLine("Count: {0}", duplicates.Count());
正下方添加了一行,并显示6
。除非我对此功能的要求有所遗漏,否则结果集合中应该只有1个项目。
ToList
为了解决这个问题,我进行了添加,但这意味着该方法在调用后立即执行,而不是在迭代结果时执行。
var hash = new HashSet<int>();
var duplicates = list.Where(i => !hash.Add(i));
将导致一个包含所有重复项的列表。因此,如果列表中有4个出现的2,那么重复列表将包含3个出现的2,因为只能将2中的一个添加到HashSet中。如果您希望列表包含每个重复项的唯一值,请改用此代码:var duplicates = mylist.Where(item => !myhash.Add(item)).ToList().Distinct().ToList();
你可以这样做:
var list = new[] {1,2,3,1,4,2};
var duplicateItems = list.Duplicates();
使用这些扩展方法:
public static class Extensions
{
public static IEnumerable<TSource> Duplicates<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector)
{
var grouped = source.GroupBy(selector);
var moreThan1 = grouped.Where(i => i.IsMultiple());
return moreThan1.SelectMany(i => i);
}
public static IEnumerable<TSource> Duplicates<TSource, TKey>(this IEnumerable<TSource> source)
{
return source.Duplicates(i => i);
}
public static bool IsMultiple<T>(this IEnumerable<T> source)
{
var enumerator = source.GetEnumerator();
return enumerator.MoveNext() && enumerator.MoveNext();
}
}
在Duplicates方法中使用IsMultiple()比Count()更快,因为这不会迭代整个集合。
Count()
]与遍历整个列表基本上不同。Count()
是预先计算的,但没有迭代整个列表。
我创建了一个扩展名以响应此问题,您可以将其包括在您的项目中,我认为当您在List或Linq中搜索重复项时,这种情况最为常见。
例:
//Dummy class to compare in list
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public Person(int id, string name, string surname)
{
this.Id = id;
this.Name = name;
this.Surname = surname;
}
}
//The extention static class
public static class Extention
{
public static IEnumerable<T> getMoreThanOnceRepeated<T>(this IEnumerable<T> extList, Func<T, object> groupProps) where T : class
{ //Return only the second and next reptition
return extList
.GroupBy(groupProps)
.SelectMany(z => z.Skip(1)); //Skip the first occur and return all the others that repeats
}
public static IEnumerable<T> getAllRepeated<T>(this IEnumerable<T> extList, Func<T, object> groupProps) where T : class
{
//Get All the lines that has repeating
return extList
.GroupBy(groupProps)
.Where(z => z.Count() > 1) //Filter only the distinct one
.SelectMany(z => z);//All in where has to be retuned
}
}
//how to use it:
void DuplicateExample()
{
//Populate List
List<Person> PersonsLst = new List<Person>(){
new Person(1,"Ricardo","Figueiredo"), //fist Duplicate to the example
new Person(2,"Ana","Figueiredo"),
new Person(3,"Ricardo","Figueiredo"),//second Duplicate to the example
new Person(4,"Margarida","Figueiredo"),
new Person(5,"Ricardo","Figueiredo")//third Duplicate to the example
};
Console.WriteLine("All:");
PersonsLst.ForEach(z => Console.WriteLine("{0} -> {1} {2}", z.Id, z.Name, z.Surname));
/* OUTPUT:
All:
1 -> Ricardo Figueiredo
2 -> Ana Figueiredo
3 -> Ricardo Figueiredo
4 -> Margarida Figueiredo
5 -> Ricardo Figueiredo
*/
Console.WriteLine("All lines with repeated data");
PersonsLst.getAllRepeated(z => new { z.Name, z.Surname })
.ToList()
.ForEach(z => Console.WriteLine("{0} -> {1} {2}", z.Id, z.Name, z.Surname));
/* OUTPUT:
All lines with repeated data
1 -> Ricardo Figueiredo
3 -> Ricardo Figueiredo
5 -> Ricardo Figueiredo
*/
Console.WriteLine("Only Repeated more than once");
PersonsLst.getMoreThanOnceRepeated(z => new { z.Name, z.Surname })
.ToList()
.ForEach(z => Console.WriteLine("{0} -> {1} {2}", z.Id, z.Name, z.Surname));
/* OUTPUT:
Only Repeated more than once
3 -> Ricardo Figueiredo
5 -> Ricardo Figueiredo
*/
}
仅查找重复值:
var duplicates = list.GroupBy(x => x.Key).Any(g => g.Count() > 1);
例如。var list = new [] {1,2,3,1,4,2};
因此group by将按其键对数字进行分组,并与之保持计数(重复的次数)。之后,我们只检查重复多次的值。
仅查找uniuqe值:
var unique = list.GroupBy(x => x.Key).All(g => g.Count() == 1);
例如。var list = new [] {1,2,3,1,4,2};
因此group by将按其键对数字进行分组,并与之保持计数(重复的次数)。之后,我们只是检查仅重复一次均值唯一的值。
var unique = list.Distinct(x => x)
在MS SQL Server中检查的完整的Linq to SQL Duplicates功能扩展集。不使用.ToList()或IEnumerable。这些查询在SQL Server中而不是在内存中执行。。结果仅在内存中返回。
public static class Linq2SqlExtensions {
public class CountOfT<T> {
public T Key { get; set; }
public int Count { get; set; }
}
public static IQueryable<TKey> Duplicates<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> groupBy)
=> source.GroupBy(groupBy).Where(w => w.Count() > 1).Select(s => s.Key);
public static IQueryable<TSource> GetDuplicates<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> groupBy)
=> source.GroupBy(groupBy).Where(w => w.Count() > 1).SelectMany(s => s);
public static IQueryable<CountOfT<TKey>> DuplicatesCounts<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> groupBy)
=> source.GroupBy(groupBy).Where(w => w.Count() > 1).Select(y => new CountOfT<TKey> { Key = y.Key, Count = y.Count() });
public static IQueryable<Tuple<TKey, int>> DuplicatesCountsAsTuble<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> groupBy)
=> source.GroupBy(groupBy).Where(w => w.Count() > 1).Select(s => Tuple.Create(s.Key, s.Count()));
}
有一个答案,但我不明白为什么不起作用;
var anyDuplicate = enumerable.GroupBy(x => x.Key).Any(g => g.Count() > 1);
在这种情况下,我的解决方案就是这样;
var duplicates = model.list
.GroupBy(s => s.SAME_ID)
.Where(g => g.Count() > 1).Count() > 0;
if(duplicates) {
doSomething();
}