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 ?
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();