所有扩展方法未返回具有两个以上属性的预期值

ALL Extension Method is not returning expected values with more than Two Properties

我会尽力解释我遇到的问题。基本上我有两个列表。

现在我要做的是 select 其他列表中不存在的所有值。但问题是,如果我使用 All 扩展方法并且 select 超过 两个 属性,它总是 return 0 值。

这是我的代码:

第一个列表:

IList<Person> lstOne = new List<Person>();
        lstOne.Add(new Person()
            {
                ID = 1, 
                IDSecond = 2,
                IDThird = 2
            });
        lstOne.Add(new Person()
            {
                ID = 2, 
                IDSecond = 3,
                IDThird = 2
            });

第二个列表:

        IList<Person> lstFinal = new List<Person>();
        lstFinal.Add(new Person()
        {
            ID = 1,
            IDSecond = 2,
            IDThird = 2
        });
        lstFinal.Add(new Person()
        {
            ID = 3,
            IDSecond = 4,
            IDThird = 2
        });
        lstFinal.Add(new Person()
        {
            ID = 4,
            IDSecond = 5,
            IDThird = 2
        });

现在如果我想select第一个列表中不存在的第二个列表,我通常这样做:

var resultOne = lstFinal.Where(c => lstOne
.All(x => x.ID != c.ID && x.IDSecond != c.IDSecond)).ToList();

这很好用,结果将是两个值:

ID: 3 and ID: 4

如果我 select 一两个属性就可以正常工作。但是如果我在我的项目中添加额外的 属性,结果总是零:

var resultOne = lstFinal.Where(c => lstOne
.All(x => x.ID != c.ID && x.IDSecond != c.IDSecond && x.IDThird != c.IDThird)).ToList();

我的问题应该是这段代码:

var resultOne = lstFinal.Where(c => lstOne
.All(x => x.ID != c.ID && x.IDSecond != c.IDSecond && x.IDThird != c.IDThird)).ToList();

Returns两个值而不是none?我期待它 return 这个,但它 return 0 个值:

ID: 3 and ID: 4 

Enumerable 已经为您定义了一些函数,因此您不必重新发明轮子

勾选这个MSDN

class Compare : IEqualityComparer<Person>
    {
        public bool Equals(Person x, Person y)
        {
            if (x == null || y == null)
            {
                return false;
            }
            return x.ID == y.ID && x.IDSecond == y.IDSecond;
        }

        public int GetHashCode(Person obj)
        {
            return 1;
        }
    }

    class FullCompare : IEqualityComparer<Person>
    {
        public bool Equals(Person x, Person y)
        {
            if (x == null || y == null)
            {
                return false;
            }
            return x.ID == y.ID && x.IDSecond == y.IDSecond && x.IDThird == y.IDThird;
        }

        public int GetHashCode(Person obj)
        {
            return 1;
        }
    }

    class Person
    {
        public int ID { get; set; }
        public int IDSecond { get; set; }
        public int IDThird { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IList<Person> lstOne = new List<Person>();
            lstOne.Add(new Person()
            {
                ID = 1,
                IDSecond = 2,
                IDThird = 2
            });
            lstOne.Add(new Person()
            {
                ID = 2,
                IDSecond = 3,
                IDThird = 2
            });

            IList<Person> lstFinal = new List<Person>();
            lstFinal.Add(new Person()
            {
                ID = 1,
                IDSecond = 2,
                IDThird = 2
            });
            lstFinal.Add(new Person()
            {
                ID = 3,
                IDSecond = 4,
                IDThird = 2
            });
            lstFinal.Add(new Person()
            {
                ID = 4,
                IDSecond = 5,
                IDThird = 2
            });


            var a = lstFinal.Except(lstOne, new Compare());
            var b = lstFinal.Except(lstOne, new FullCompare());
            a.ToList().ForEach(s => Console.WriteLine(s.ID));
            b.ToList().ForEach(s => Console.WriteLine(s.ID));

            Console.ReadLine();
            //Console.WriteLine();
        }
    }