简体   繁体   English

EF6中的两个列表之间的区别,使用except不起作用

[英]Difference Between Two Lists in EF6 using except doesn't work

I am trying to calculate the Difference between two list my list type has this structure : 我试图计算两个列表之间的差异,我的列表类型具有以下结构:

public partial class Assistance
{

    public int Id { get; set; }
    public string AssistanceName { get; set; }
    public string AssistanceTell { get; set; }
}

and here is my query : 这是我的查询:

           List<Assistance> assistanceWithoutExpert;
            AssistanceJurorRepository assistanceJurorRepository = new AssistanceJurorRepository();

            List<Assistance> assistancesWithExpert = assistanceJurorRepository.FindBy(i => i.User.Permission == "Assistance").Select(i => i.Assistance).ToList();
            List<Assistance> AllAssitance = GetAll().ToList();
            assistanceWithoutExpert = AllAssitance.Except(assistancesWithExpert).ToList();
            return assistanceWithoutExpert;

As you can see i have a list that holds all assitances called AllAssitance and a list that hold assitances that has expert called assistancesWithExpert i need to calculate assistanceWithoutExpert .So but after executing the result is all records in AllAssitance why ? 正如你可以看到我有持有召集所有assitances列表AllAssitance并持有assitances已经列表expert称为assistancesWithExpert我需要计算assistanceWithoutExpert 。所以,但执行的结果后的所有记录在AllAssitance为什么呢?

Best regards 最好的祝福

You need to use the overload which takes a comparer . 您需要使用需要比较器重载 It doesn't work out of the box with complex types. 对于复杂类型,它开箱即用。

public class AssistanceComparer: IEqualityComparer<Assistance>
{
    public bool Equals(Assistance x, Assistance y)
    {
        return x.ID == y.ID;
    }

    public int GetHashCode(Assistance assistance)
    {
        return assistance.ID.GetHashCode();
    }
}

Usage: 用法:

assistanceWithoutExpert = AllAssitance.Except(assistancesWithExpert, new AssistanceComparer()).ToList();

The comparison is done using the default equality comparer. 使用默认的相等比较器完成比较。 From MSDN : MSDN

Produces the set difference of two sequences by using the default equality comparer to compare values. 通过使用默认的相等比较器比较值来产生两个序列的集合差异。

You need to define a custom comparison by overriding GetHashCode and Equals . 您需要通过覆盖GetHashCodeEquals来定义自定义比较。

The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer generic interface. 默认的相等比较器Default用于比较实现IEqualityComparer通用接口的类型的值。 To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type. 要比较自定义数据类型,您需要实现此接口并为该类型提供自己的GetHashCode和Equals方法。

In short, you need to add the following code to your class Assistence if you want to compare the instances through the Id property. 简而言之,如果您想通过Id属性比较实例,则需要在Assistence类中添加以下代码。

public override int GetHashCode() {
    return Id.GetHashCode();
}

public override bool Equals(obj otherInstance) {
    return (otherInstance is Assistence) && ((Assistence)otherInstance).Id == Id;
}

A note of warning though if you want to compare on some field that isn't unique. 但是,如果您要在不唯一的某个字段上进行比较,则需要注意警告。 See this link . 看到这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM