c#中对象列表的异或 [英] xor of list of objects in c#

查看:46
本文介绍了c#中对象列表的异或的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c# 中的对象之间进行异或运算时遇到问题.假设我有一个具有以下属性的类 -

Hi i have an issue while doing xoring between objects in c#. Suppose i have a class having below properties -

public string UserName { get; set; }
public bool IsUserEmployed { get; set; }
public bool IsUserValid { get; set; }`

我有 2 个此类的列表:

I have 2 lists of this classes:

List<Class1> List1 = new List<Class1>();
List<Class1> List2 = new List<Class1>();

我尝试了如下的基本异或运算:

I tried the basic xoring as follows:

 FinalList.AddRange(List1.Except(List2));                  
 FinalList.AddRange(List2.Except(List1));`

但我没有在 FinalList 中得到结果作为异或.

But i didnt got the results in the FinalList as xor.

我想对 List1List2 执行异或,它可以比较两个列表中对象的所有 3 个属性,并给我第三个列表.请帮忙.

I want to perform xor on the List1 and List2 that could compare all the 3 properties in the objects in both the list and give me a 3rd list. Kindly Help.

推荐答案

这是一个在 LinqPad 中拼凑起来的解决方案.我创建了一个自定义的IEqualityComparerm,可以随意复用.为了使用这个比较器,我将 2 个参数传递给 Except 方法:第二个列表和比较器.

This is a solution scratched together in LinqPad. I have created a custom IEqualityComparerm, which can be reused at will. To use this comparer I am passing 2 arguments to the Except method: the second list and the comparer.

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);
  }
}

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

As a small note: This code does not check for null or other possible errors.

这篇关于c#中对象列表的异或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆