C# c语言中对象列表的异或#

C# c语言中对象列表的异或#,c#,xor,C#,Xor,嗨,我在c#中的对象之间执行xoring时遇到问题。 假设我有一个具有以下属性的类- public string UserName { get; set; } public bool IsUserEmployed { get; set; } public bool IsUserValid { get; set; }` 我有两个此类的列表: List<Class1> List1 = new List<Class1>(); List<Class1> List2

嗨,我在c#中的对象之间执行xoring时遇到问题。 假设我有一个具有以下属性的类-

public string UserName { get; set; }
public bool IsUserEmployed { get; set; }
public bool IsUserValid { get; set; }`
我有两个此类的列表:

List<Class1> List1 = new List<Class1>();
List<Class1> List2 = new List<Class1>();
但是我没有在
FinalList
中作为xor得到结果


我想对
List1
List2
执行异或运算,这样可以比较两个列表中对象的所有3个属性,并给出第三个列表。请提供帮助。

您需要覆盖
Class1
的Equals方法

为什么??因为现在它正在比较实例,我猜在你的例子中,它们总是返回false

public override bool Equals(Object obj)
{
    Class cs = (MyClass)obj;
    return UserName == cs.UserName && 
           IsUserEmployed  = cs.IsUserEmployed &&
           IsUserValid == cs.IsUserValid;
}

**您需要在obj不为null的地方进行一些检查。

这是一个在LinqPad中拼凑起来的解决方案。我创建了一个定制的IEqualityComparerm,可以随意重用。要使用此比较器,我将向
方法传递两个参数,但
方法除外:第二个列表和比较器

void Main()
{
    List<CustomObject> List1 = new List<CustomObject>() 
    {
        new CustomObject() {UserName ="1", IsUserEmployed = true, IsUserValid = false},
        new CustomObject() {UserName ="2", IsUserEmployed = true, IsUserValid = false},
        new CustomObject() {UserName ="3", IsUserEmployed = true, IsUserValid = false},
        new CustomObject() {UserName ="4", IsUserEmployed = true, IsUserValid = false}
    };
    List<CustomObject> List2 = new List<CustomObject>() 
    {
        new CustomObject() {UserName ="2", IsUserEmployed = true, IsUserValid = false},
        new CustomObject() {UserName ="3", IsUserEmployed = true, IsUserValid = false},

    };

    IEqualityComparer<CustomObject> CustomComparer = new CustomObjectEqualityComparer<CustomObject>();

    var xor = List1.Except(List2, CustomComparer).ToList().Dump();


}
public class CustomObject 
{
    public string UserName { get; set; }
    public bool IsUserEmployed { get; set; }
    public bool IsUserValid { get; set; }
}


public class CustomObjectEqualityComparer<T> : IEqualityComparer<CustomObject>
{
    public bool Equals(CustomObject t1, CustomObject t2)
    {
        if(t1.IsUserEmployed == t2.IsUserEmployed &&
           t1.IsUserValid ==t2.IsUserValid &&
           t1.UserName == t2.UserName)
       {
            return true;
       }

        return false;
    }

    public int GetHashCode(CustomObject _obj)
    {
        return _obj.IsUserEmployed.GetHashCode() + _obj.IsUserEmployed.GetHashCode() + _obj.IsUserValid.GetHashCode();
    }
}
public static class IEnumerableExtensions
{
  public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
  {
    foreach (T item in source)
      action(item);
  }
}
void Main()
{
List List1=新列表()
{
新建CustomObject(){UserName=“1”,IsUserEmployed=true,IsUserValid=false},
新建CustomObject(){UserName=“2”,IsUserEmployed=true,IsUserValid=false},
新建CustomObject(){UserName=“3”,IsUserEmployed=true,IsUserValid=false},
新建CustomObject(){UserName=“4”,IsUserEmployed=true,IsUserValid=false}
};
List List2=新列表()
{
新建CustomObject(){UserName=“2”,IsUserEmployed=true,IsUserValid=false},
新建CustomObject(){UserName=“3”,IsUserEmployed=true,IsUserValid=false},
};
IEqualityComparer CustomComparer=新的CustomObjectEqualityComparer();
var xor=List1.Except(List2,CustomComparer.ToList().Dump();
}
公共类CustomObject
{
公共字符串用户名{get;set;}
公共bool IsUserEmployed{get;set;}
公共bool IsUserValid{get;set;}
}
公共类CustomObjectEqualityComparer:IEqualityComparer
{
公共布尔等于(CustomObject t1、CustomObject t2)
{
如果(t1.IsUserEmployed==t2.IsUserEmployed&&
t1.IsUserValid==t2.IsUserValid&&
t1.UserName==t2.UserName)
{
返回true;
}
返回false;
}
public int GetHashCode(CustomObject\u obj)
{
返回_obj.IsUserEmployed.GetHashCode()+_obj.IsUserEmployed.GetHashCode()+_obj.IsUserValid.GetHashCode();
}
}
公共静态类IEnumerableExtensions
{
公共静态void ForEach(此IEnumerable源、操作)
{
foreach(源中的T项)
行动(项目);
}
}

请注意:此代码不检查null或其他可能的错误。

fast xor
ILST
集合的替代实现

public static IList<T> ExclusiveOr<T>([NotNull]this IList<T> @this, IList<T> a)
{
    a = a ?? new T[]{};
    ISet<T> set = new HashSet<T>(@this);
    foreach (T current in a)
    {
        if (set.Contains(current))
        {
            set.Remove(current);
        }
        else
        {
            set.Add(current);
        }
    }
    return set.ToList();
}
public static IList ExclusiveOr([NotNull]this IList@this,IList a)
{
a=a??新的T[]{};
ISet set=新哈希集(@this);
foreach(T电流单位:a)
{
if(集合包含(当前))
{
设置。移除(当前);
}
其他的
{
设置。添加(当前);
}
}
返回set.ToList();
}

两个列表之间有一个XOR、左XOR或右XOR的扩展:


您可以选择是否将其与
IEqualityComparer
一起使用。

首先,您应该为您的类重写
Equals
GetHashCode
。这将允许您比较对象。查看此帖子,不客气。我刚刚注意到,IEnumerable有一个ExtensionMethod,可以在方法语法中使用ForEach。我的模板中的剩余内容,请忽略它。
public static IList<T> ExclusiveOr<T>([NotNull]this IList<T> @this, IList<T> a)
{
    a = a ?? new T[]{};
    ISet<T> set = new HashSet<T>(@this);
    foreach (T current in a)
    {
        if (set.Contains(current))
        {
            set.Remove(current);
        }
        else
        {
            set.Add(current);
        }
    }
    return set.ToList();
}