关于C#List数组取交集的问题 5C

代码如下:

public class Group
{
        public List<Field> FieldList { get; set; } = new List<Field>();
        public string GroupId { get; set; }
        public string GroupName { get; set; }
        //重写Equals
        public override bool Equals(object obj)
        {
                Group group = obj as Group;
                if (group == null) return false;
                if (this.GetType() != group.GetType()) return false;
                return this.FieldList.Equals(group.FieldList);
        }

        public override int GetHashCode()
        {
                return this.FieldList.GetHashCode();
        }

public class Field
{
        public bool ContactClientFlag { get; set; }
        public bool ContactFlag { get; set; }
        //重写equals
        public override bool Equals(object obj)
        {
                    Field field = obj as Field;
                    if (this.GetType() != field.GetType()) return false;    
                    return (this.ContactClientFlag == field.ContactClientFlag)&&(this.ContactFlag== field.ContactFlag;  
        }
        public override int GetHashCode()
        {
                                    int hashCode = this.ContactClientFlag.GetHashCode();
                hashCode += this.ContactFlag.GetHashCode();
                return hashCode;
        }               
}
上述为两个类,Group和Field类。其中 Group类的一个属性是List<Field>类型。
我生成两个List<Group>类型数组,求交集代码如下:

Field field_1 =new Field(){
    ContactClientFlag = FALSE,
    ContactFlag = TRUE
};
List<Field> fieldList_1 = new List<Field>();
fieldList_1.Add(field_1);
Group group_1 =new Group(){
    GroupId = "222",
    GroupName = "test",
    FieldList = fieldList_1
};
List<Group> groupList_1 = new List<Group>();
groupList_1.Add(group_1);

Field field_2 =new Field(){
    ContactClientFlag = FALSE,
    ContactFlag = TRUE
};
List<Field> fieldList_2 = new List<Field>();
fieldList_2.Add(field_2);
Group group_2 =new Group(){
    GroupId = "222",
    GroupName = "test",
    FieldList = fieldList_2
};
List<Group> groupList_2 = new List<Group>();
groupList_2.Add(group_2);

public class GroupEquality : IEqualityComparer<Group>
{
        public bool Equals(Group left, Group right)
        {
                return left.FieldList.Equals(right.FieldList);
        }

        public int GetHashCode(IntelUserModel obj)
        {
                return obj.FieldList.GetHashCode();
        }
}
List<Group> noChange = groupList_1.Intersect(groupList_2, new GroupEquality()).ToList();

可是求出来的noChange数组的Count属性为0,该怎么办呢?

2个回答

public class Group
{
        public override bool Equals(object obj)
        {
                        ...
                return this.FieldList.Equals(group.FieldList);  
                //1. 使用  SequenceEqual(group.FieldList)
        }

}

public class GroupEquality : IEqualityComparer<Group>
{
        public bool Equals(Group left, Group right)
        {
                return left.FieldList.Equals(right.FieldList);
                //2.使用  SequenceEqual(group.FieldList)
        }

        public int GetHashCode(IntelUserModel obj)
        {
                return obj.FieldList.GetHashCode();
                //3.return 1;
        }
}

至少这3个地方要改,你再跑下;

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问