Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 合并两个没有DUP的列表_C#_List_Optimization_Duplicates_Duplicate Removal - Fatal编程技术网

C# 合并两个没有DUP的列表

C# 合并两个没有DUP的列表,c#,list,optimization,duplicates,duplicate-removal,C#,List,Optimization,Duplicates,Duplicate Removal,我需要在其他两个列表的基础上创建一个列表。但它似乎没有删除重复项 这是合并两个无重复列表的有效方法吗 List<String[]> blocksComparisonSet1 = new List<String[]>(); List<String[]> blocksComparisonSet2 = new List<String[]>(); //we will combine list1 and list2 into this one List&

我需要在其他两个列表的基础上创建一个列表。但它似乎没有删除重复项

这是合并两个无重复列表的有效方法吗

List<String[]> blocksComparisonSet1 = new List<String[]>(); 
List<String[]> blocksComparisonSet2 = new List<String[]>(); 
//we will combine list1 and list2 into this one
List<String[]> blocksComparisonFinal = new List<String[]>(); 

//this is how I store data in each list  
//if both values found, add both of them (partial functions, FYI)
String[] NA = new String[2]; //keep results
NA[0] = baseLine; //[0] for base
NA[1] = resultLine; //[1] for result
blocksComparisonSet1.Add(NA);

//if only one value found
String[] NA = new String[2]; //keep results
NA[0] = ""; //[0] for base
NA[1] = resultLine; //[1] for result
blocksComparisonSet1.Add(NA);

//This is where I merge lists and try to remove duplicates
if (blocksComparisonSet1.Count() > 0 || blocksComparisonSet2.Count() > 0) 
//check if we have any values in out differences lists. if we do, merge them
{
    blocksComparisonFinal.AddRange(blocksComparisonSet1); 
    //add records from one list to final list
    blocksComparisonFinal.AddRange(blocksComparisonSet2); 
    //add records from second list to final list
    blocksComparisonFinal = blocksComparisonFinal.Distinct().ToList(); 
    //remove dublicates
}
-

-

var merge=blocksComparisonSet1.Union(
区块比较集2,
新阵列质量比较程序()
).ToList();
您需要一个自定义的
IEqualityComparer
。看

下面是一个实现:

class ArrayEqualityComparer<T> : IEqualityComparer<T[]> {
    public bool Equals(T[] x, T[] y) {
        if(Object.ReferenceEquals(x, y)) {
            return true;
        }
        if(x == null || y == null) {
            return false;
        }
        return x.SequenceEqual(y);
    }

    public int GetHashCode(T[] x) {
        if(x == null) {
            return 0; 
        }
        return x.Aggregate(
            0,
            (h, item) => h ^ (item != null ? item.GetHashCode() : 0)
        );
    }
}
class ArrayEqualityComparer:IEqualityComparer{
公共布尔等于(T[]x,T[]y){
if(Object.ReferenceEquals(x,y)){
返回true;
}
如果(x==null | | y==null){
返回false;
}
返回x.x(y);
}
公共int GetHashCode(T[]x){
如果(x==null){
返回0;
}
报税表x.合计(
0,
(h,item)=>h^(item!=null?item.GetHashCode():0)
);
}
}

一种替代的非Linq解决方案,实现了
IEqualityComparer

var merged=newhashset(blocksComparisonSet1,new SEC());
合并。与(区块比较集2)合并;
类别:IEqualityComparer
{
公共布尔等于(字符串[]p1,字符串[]p2){
返回p1.SequenceEqual(p2);
}
public int GetHashCode(字符串[]p){
返回(int)p.Sum(p1=>p1.GetHashCode());
}
}

当您指的是
Any
时,不要使用
Count
。是的,您肯定需要实现IEqualityComparer。你的代码不能正常工作。小心!数组中可能存在null
string
引用。
List2[]  Example    
na  string2
na  string5
String1 string3
String7 string4
string8 string5
string9 na
string2 na
Final List[] must be:
na  string9
na  string2
na  string5
string1 na
String1 string3
string2 na
string3 String1
string4 String7
string5 string8
String7 string4
string8 string5
string9 na
var merge = blocksComparisonSet1.Union(
                blocksComparisonSet2,
                new ArrayEqualityComparer<string>()
            ).ToList();
class ArrayEqualityComparer<T> : IEqualityComparer<T[]> {
    public bool Equals(T[] x, T[] y) {
        if(Object.ReferenceEquals(x, y)) {
            return true;
        }
        if(x == null || y == null) {
            return false;
        }
        return x.SequenceEqual(y);
    }

    public int GetHashCode(T[] x) {
        if(x == null) {
            return 0; 
        }
        return x.Aggregate(
            0,
            (h, item) => h ^ (item != null ? item.GetHashCode() : 0)
        );
    }
}
var merged = new HashSet<string[]>(blocksComparisonSet1, new SEC());
merged.UnionWith(blocksComparisonSet2);

class SEC : IEqualityComparer<string[]>
{
    public bool Equals(string[] p1, string[] p2){
        return p1.SequenceEqual(p2);
    }
    public int GetHashCode(string[] p){ 
        return (int)p.Sum (p1 => p1.GetHashCode()); 
    }
}