天使半只翼

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于Union的两种情况

一、简单值类型或者string类型处理方式(集合需要实现IEnumerable接口)

        #region int类型

            List<int> ints1 = new List<int> { 1, 2, 3, 4, 5, 6 };
            List<int> ints2 = new List<int> { 5, 6, 7, 8, 9, 0 };
            IEnumerable<int> intsResult = ints1.Union(ints2).OrderBy(c => c);
            //结果为0, 1, 2, 3, 4, 5, 6, 7, 8, 9

            #endregion

            #region string类型

            List<string> str1 = new List<string> { "str1", "str2", "str3", "str4", "str5", "str6" };
            List<string> str2 = new List<string> { "str5", "str6", "str7", "str8", "str9", "str0" };
            IEnumerable<string> strResult = str1.Union(str2).OrderBy(c => c);
            //结果为str0, str1, str2, str3, str4, str5, str6, str7, str8, str9

            #endregion

 

二、引用类型(自定义类类型),这时我们就需要定义自己的比较方式,否则编译器会以默认的比较对象的应用类型,有时并不能得到我们需要的结果。

1:首先需要定义比较类型,需要实现IEqualityComparer或者其返修接口IEqualityComparer<T>

      a:比如我们有一个自定义类,我们要建立对它的Union操作,该类型定义如下:

 

    public class User
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Grade { get; set; }

        public int CompareTo(User stu)
        {
            if (stu == null)
                throw new ArgumentNullException("stu");
            return Grade.CompareTo(stu.Grade);
        }
    }

b:自定义比较类型如下:

    /// <summary>
    /// 自定义比较类
    /// </summary>
    public class EntityComparer : IEqualityComparer<User>
    {
        public bool Equals(User a, User b)
        {
            if (Object.ReferenceEquals(a, b)) return true;
            if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(b, null))
                return false;

            return a.ID == b.ID && a.Name == b.Name && a.Grade == b.Grade;
        }

        public int GetHashCode(User a)
        {
            if (Object.ReferenceEquals(a, null)) return 0;
            int hashName = a.Name == null ? 0 : a.Name.GetHashCode();
            int hashCode = a.Grade.GetHashCode();

            return hashName ^ hashCode;
        }
    }

2:建立对User对象集合的Union关系(应用我们自定义的EntityComparer):

            #region 对象集合


            IEnumerable<User> list1 = new List<User> { new User { ID = "0003", Name = "张三", Grade = "高三" }, new User { ID = "0002", Name = "王二", Grade = "高三" } };
            IEnumerable<User> list2 = new List<User> { new User { ID = "0003", Name = "张三", Grade = "高三" }, new User { ID = "0004", Name = "李四", Grade = "高二" } };

            IEqualityComparer<User> ec = new EntityComparer();

            //1:直接应用Union的重装方法
            //IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
            IEnumerable<User> users1 = list1.Union(list2,ec).OrderBy(c=>c.ID);

            //IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
            //2:使用Concat,然后再使用distinct方法
            IEnumerable<User> users2 = list1.Concat(list2).Distinct(ec).OrderBy(c => c.ID);

            foreach (User item in users2)
                Console.WriteLine("ID:{0,-7}Name:{1,-5}Grade:{2}", item.ID, item.Name, item.Grade);

            /*结果
            ID = "0002", Name = "王二", Grade = "高三"
            ID = "0003", Name = "张三", Grade = "高三"
            ID = "0004", Name = "李四", Grade = "高二"
            */

 

posted on 2014-01-03 13:24  天使半只翼  阅读(4923)  评论(0编辑  收藏  举报