[英]C# remove duplicates from List<string[]>
I got a lot of data from a database, which are results from a search function. 我从数据库中获得了很多数据,这些数据是搜索功能的结果。 Now I've a
List<string[]>
which has duplicated elements of type string[]
. 现在,我有一个
List<string[]>
,它具有重复的string[]
类型的元素。 The string[]
in the list are the search results. 列表中的
string[]
是搜索结果。
I know that every new created array has a different instance so i can't use MyListOfArrays.Distinct().ToList()
. 我知道每个新创建的数组都有一个不同的实例,所以我不能使用
MyListOfArrays.Distinct().ToList()
。
Maybe it's a very basic question... 也许这是一个非常基本的问题...
My question is, are there any functions built in to remove a duplicated string[]
form the List<string[]>
? 我的问题是,是否有内置函数从
List<string[]>
删除重复的string[]
? Or do I have to write it by my selfe? 还是我必须自己写?
Thank you 谢谢
You can use distinct
method with custom equalityComparer 您可以对自定义的equalsComparer使用
distinct
方法
IEnumerable<string[]> distinct = inputStringArrayList.Distinct(new EqualityComparer());
EqualityComparer 平等比较器
class EqualityComparer : IEqualityComparer<string[]>
{
public bool Equals(string[] x, string[] y)
{
if (x.Length != y.Length)
{
return false;
}
if (x.Where((t, i) => t != y[i]).Any())
{
return false;
}
return true;
}
public int GetHashCode(string[] obj)
{
return obj.GetHashCode();
}
}
Alternative Equals Method 替代平等法
public bool Equals(string[] x, string[] y)
{
return x.SequenceEqual(y);
}
Here I am assuming you are having exact same string arrays with same content at same index. 在这里,我假设您在相同的索引处具有完全相同的字符串数组,并且内容相同。
Correction from Matthew Watson Matthew Watson的改正
public int GetHashCode(string[] obj)
{
if (obj == null)
return 0;
int hash = 17;
unchecked
{
foreach (string s in obj)
hash = hash*23 + ((s == null) ? 0 : s.GetHashCode());
}
return hash;
}
I have corrected the answer from @Muctadir Dinar. 我已经更正了@Muctadir Dinar的答案。
(He deserves credit for the answer - I am just correcting it and providing a complete test program): (他的答案值得称赞-我只是在更正并提供完整的测试程序):
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo
{
sealed class EqualityComparer: IEqualityComparer<string[]>
{
public bool Equals(string[] x, string[] y)
{
if (ReferenceEquals(x, y))
return true;
if (x == null || y == null)
return false;
return x.SequenceEqual(y);
}
public int GetHashCode(string[] obj)
{
if (obj == null)
return 0;
int hash = 17;
unchecked
{
foreach (string s in obj)
hash = hash*23 + ((s == null) ? 0 : s.GetHashCode());
}
return hash;
}
}
class Program
{
private void run()
{
var list = new List<string[]>
{
strings(1, 10),
strings(2, 10),
strings(3, 10),
strings(2, 10),
strings(4, 10)
};
dump(list);
Console.WriteLine();
var result = list.Distinct(new EqualityComparer());
dump(result);
}
static void dump(IEnumerable<string[]> list)
{
foreach (var array in list)
Console.WriteLine(string.Join(",", array));
}
static string[] strings(int start, int count)
{
return Enumerable.Range(start, count)
.Select(element => element.ToString())
.ToArray();
}
static void Main(string[] args)
{
new Program().run();
}
}
}
A simple and not very efficient approach would be to use string.Join
on the string[]
: 一种简单但不是非常有效的方法是使用
string.Join
加入string[]
:
list = list
.GroupBy(strArr => string.Join("|", strArr))
.Select(g => g.First())
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.