C# 从自定义类数组中包含的对象[]中删除项

C# 从自定义类数组中包含的对象[]中删除项,c#,arrays,linq,list,C#,Arrays,Linq,List,我有一个自定义项数组,然后有一个由函数返回的对象数组object[]。这个对象数组确实是相同的客户端类型,但它作为对象类型的数组返回(我无法编辑这个特性) 我尝试使用显式强制转换,但在运行时遇到一个异常:无法将对象[]转换为客户端[]一旦我拥有两个数组,我的目标就是从一个列表中删除另一个列表中的项目,并建立一个联合,以获得一个唯一的列表 var client = getClients().ToArray(); //Is an array of Clients[] var no

我有一个自定义项数组,然后有一个由函数返回的对象数组object[]。这个对象数组确实是相同的客户端类型,但它作为对象类型的数组返回(我无法编辑这个特性)

我尝试使用显式强制转换,但在运行时遇到一个异常:无法将对象[]转换为客户端[]
一旦我拥有两个数组,我的目标就是从一个列表中删除另一个列表中的项目,并建立一个联合,以获得一个唯一的列表

var client = getClients().ToArray();           //Is an array of Clients[]
var normalSearch = getDuplicates().ToArray();  //Is an array of object[]


//This what I try to achieve,but being object, I cannot invoke "c.ContactId" (Line 4)
var uni = (from c in normalSearch
           where !(from d in dupes
                   select d.ContactId)
                   .Contains(c.ContactId)
           select c).ToArray();
我知道在Linq中,如果使用基元类型,Union()可以自动排除重复,否则必须使用自定义类型开发扩展。但我无法访问代码的其余部分,因此不可能在其他地方更改逻辑

var uni = client.Union(normalSearch.Cast<Client>())
    .DistinctBy(c => c.ContractId);
这基本上就是它的实现;
getDuplicates().Cast<Client>();

您可以使用linq将副本强制转换到客户端,然后创建一个比较器类。我从中复制并粘贴了这个,它可能需要调整

var normalSearch = getDuplicates().ToArray().Cast<Clients>();
var client = getClients().ToArray();

var unique = client.Union(normalSearch, new ClientsComparer());

public class ClientsComparer : IEqualityComparer<Clients>
{
    public bool Equals(Clients x, Clients y)
    {

        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal.
        return x.ContactId == y.ContactId;
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(Client client)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(product, null)) return 0;

        //Calculate the hash code for the product.
        return client.ContactId.GetHashCode();
    }

}
var normalSearch=getDuplicates().ToArray().Cast();
var client=getClients().ToArray();
var unique=client.Union(normalSearch,newclientscomparer());
公共类客户端比较:IEqualityComparer
{
公共布尔等于(客户机x、客户机y)
{
//检查比较对象是否引用相同的数据。
if(Object.ReferenceEquals(x,y))返回true;
//检查是否有任何比较对象为空。
if(Object.ReferenceEquals(x,null)| | Object.ReferenceEquals(y,null))
返回false;
//检查产品的属性是否相等。
返回x.ContactId==y.ContactId;
}
//对于一对对象,If Equals()返回true
//然后GetHashCode()必须为这些对象返回相同的值。
public int GetHashCode(客户端)
{
//检查对象是否为空
if(Object.ReferenceEquals(product,null))返回0;
//计算产品的哈希代码。
返回client.ContactId.GetHashCode();
}
}

我得到了一个关于DistinctBy的异常,因为我的IEnumerable集合没有它的定义。它是在System.Linq以外的额外库中还是我必须编写扩展?@Luca它在我链接到的MoreLinq库中。@Luca它是开源的。您只需将DistinctBy的源复制到您的解决方案中即可。这是一个有用的扩展。我知道这个解决方案,但我不能在这部分引入新代码。
var normalSearch = getDuplicates().ToArray().Cast<Clients>();
var client = getClients().ToArray();

var unique = client.Union(normalSearch, new ClientsComparer());

public class ClientsComparer : IEqualityComparer<Clients>
{
    public bool Equals(Clients x, Clients y)
    {

        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal.
        return x.ContactId == y.ContactId;
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(Client client)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(product, null)) return 0;

        //Calculate the hash code for the product.
        return client.ContactId.GetHashCode();
    }

}