Linq通过自定义函数取差集

using System;
using System.Collections.Generic;
using System.Linq;

namespace CompareList
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ObjClass[] fruits1 =
            {
                new ObjClass{ ID="张三",Ver="",Three=1},
                new ObjClass{ ID="张三",Ver="",Three=1},
                new ObjClass{ ID="张三",Ver="",Three=1},
                new ObjClass{ ID="张三",Ver="",Three=1},
                new ObjClass{ ID="李四",Ver="",Three=1},
                new ObjClass{ ID="李四",Ver="",Three=1},
                new ObjClass{ ID="李四",Ver="",Three=1},
                new ObjClass{ ID="王五",Ver="",Three=1},
                new ObjClass{ ID="王五",Ver="",Three=1}
            };

            var temp = fruits1.Select((m, i) =>
            {
                m.Index = i;
                return m;
            });

            ObjClass[] fruits2 = temp
                .GroupBy(m => new { m.ID, m.Ver, m.Three }).Select(m => new ObjClass
                {
                    Three = m.Key.Three,
                    ID = m.Key.ID,
                    Ver = m.Key.Ver, 
                    Index = m.First().Index
                }).ToArray();

            // Get all the elements from the first array
            // except for the elements from the second array.

            IEnumerable<ObjClass> except =
                temp.Except(fruits2, new ProductComparer());

            foreach (var product in except)
                Console.WriteLine(product.ID + " " + product.Ver + " " + product.Three);

            Console.ReadLine();

        }
    }

    public class ObjClass
    {
        public int Index { get; set; }
        public string ID { get; set; }
        public string Ver { get; set; }
        public int Three { get; set; }
    }

    // Custom comparer for the Product class
    class ProductComparer : IEqualityComparer<ObjClass>
    {
        // Products are equal if their names and product numbers are equal.
        public bool Equals(ObjClass x, ObjClass y)
        {

            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the products' properties are equal.
            return x.ID == y.ID && x.Ver == y.Ver && x.Three == y.Three && x.Index == y.Index;
        }

        // If Equals() returns true for a pair of objects
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(ObjClass product)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(product, null)) return 0;

            //Get hash code for the Name field if it is not null.
            int hashProductName = product.ID == null ? 0 : product.ID.GetHashCode();

            //Get hash code for the Code field.
            int hashProductCode = product.Ver.GetHashCode();

            int hashThree = product.Three.GetHashCode();

            int hashIndex = product.Index.GetHashCode();

            //Calculate the hash code for the product.
            return hashProductName ^ hashProductCode ^ hashThree ^ hashIndex;
        }
    }
}

 

posted on 2022-12-23 21:50  静以修身俭以养德  阅读(53)  评论(0编辑  收藏  举报

导航