Linq GroupBy操作

 
Linq GroupBy操作。

  1.   #region GroupBy Operator
  2.         /* GroupBy第一种原型
  3.          * 
  4.          * 返回值为IGroupting<K,T>集合数组,每个IGroupting<k,t>包含一个(K)Key及
  5.          * 一个拥有相同Key的集合数组T
  6.          * 
  7.          * 将含有相同的Key 分成一组
  8.          * public static IEnumerable<IGrouping<K, T>> GroupBy<T, K>(
  9.          *   this IEnumerable<T> source,
  10.          *   Func<T, K> keySelector);
  11.          * 
  12.          */
  13.         static void GroupByFirstPrototype()
  14.         {
  15.             PresidentsOption[] presidentsOptions ={new PresidentsOption(1,10),
  16.                                                 new PresidentsOption(2,10),
  17.                                                 new PresidentsOption(2,50),
  18.                                                 new PresidentsOption(3,100),
  19.                                                 new PresidentsOption(4,30),
  20.                                                 new PresidentsOption(4,80),
  21.                                                 new PresidentsOption(5,200),
  22.                                                 new PresidentsOption(5,10),
  23.                                                 new PresidentsOption(6,90),
  24.                                                 new PresidentsOption(6,80),
  25.                                                 new PresidentsOption(7,10)};
  26.             IEnumerable<IGrouping<int, PresidentsOption>> outerSequence = presidentsOptions.GroupBy(p => p.Id);
  27.             foreach (IGrouping<int, PresidentsOption> keyGroupSequence in outerSequence)
  28.             {
  29.                 Console.WriteLine("-----------------------------------");
  30.                 Console.WriteLine("....key(id):" + keyGroupSequence.Key);
  31.                 foreach (PresidentsOption presidents in keyGroupSequence)
  32.                 {
  33.                     Console.WriteLine("id={0}...option={1}", presidents.Id, presidents.Option);
  34.                 }
  35.             }
  36.         }
  37.         /*GroupBy第二种原型
  38.          * 
  39.          * 功能和第一种一样,只是比较方法 comparer自定义
  40.          * 需要实现IEqualityComparer<K>接口
  41.          * 
  42.          * public static IEnumerable<IGrouping<K, T>> GroupBy<T, K>(
  43.          *   this IEnumerable<T> source,
  44.          *   Func<T, K> keySelector,
  45.          *   IEqualityComparer<K> comparer);
  46.          * 
  47.          */
  48.         static void GroupBySecondPrototype()
  49.         {
  50.             PresidentsOption[] presidentsOptions ={new PresidentsOption(1,10),
  51.                                                 new PresidentsOption(2,10),
  52.                                                 new PresidentsOption(2,50),
  53.                                                 new PresidentsOption(3,100),
  54.                                                 new PresidentsOption(4,30),
  55.                                                 new PresidentsOption(4,80),
  56.                                                 new PresidentsOption(5,200),
  57.                                                 new PresidentsOption(5,10),
  58.                                                 new PresidentsOption(6,90),
  59.                                                 new PresidentsOption(6,80),
  60.                                                 new PresidentsOption(7,10)};
  61.             GroupByCompare compare=new GroupByCompare();
  62.             //通过compare比较.具有相同hashCode的Key分成一组
  63.             IEnumerable<IGrouping<int, PresidentsOption>> outerSequence = presidentsOptions.GroupBy(p => p.Id, compare);
  64.             foreach (IGrouping<int, PresidentsOption> keyGroupSequence in outerSequence)
  65.             {
  66.                 
  67.                 Console.WriteLine("-----------------------------------");
  68.                 Console.WriteLine("....key(hashCode):" + keyGroupSequence.Key);
  69.                 foreach (PresidentsOption presidents in keyGroupSequence)
  70.                 {
  71.                     Console.WriteLine("id={0}...option={1}" , presidents.Id, presidents.Option);
  72.                 }
  73.             }
  74.         }
  75.         /* GroupBy第三种原型
  76.          * 
  77.          * 第三钟原型和第一种类似,只不过IGrouping<K, E>中E值为T的一个元素,
  78.          * 而不是是T类型的实体对象
  79.          * 
  80.          * public static IEnumerable<IGrouping<K, E>> GroupBy<T, K, E>(
  81.          *    this IEnumerable<T> source,
  82.          *    Func<T, K> keySelector,
  83.          *    Func<T, E> elementSelector);
  84.          * 
  85.          */
  86.         static void GroupByThirdPrototype()
  87.         {
  88.             PresidentsOption[] presidentsOptions ={new PresidentsOption(1,10),
  89.                                                 new PresidentsOption(2,10),
  90.                                                 new PresidentsOption(2,50),
  91.                                                 new PresidentsOption(3,100),
  92.                                                 new PresidentsOption(4,30),
  93.                                                 new PresidentsOption(4,80),
  94.                                                 new PresidentsOption(5,200),
  95.                                                 new PresidentsOption(5,10),
  96.                                                 new PresidentsOption(6,90),
  97.                                                 new PresidentsOption(6,80),
  98.                                                 new PresidentsOption(7,10)};
  99.             IEnumerable<IGrouping<intint>> outerSequence = presidentsOptions.GroupBy(p => p.Id,p=>p.Option);
  100.             foreach (IGrouping<intint> keyGroupSequence in outerSequence)
  101.             {
  102.                 Console.WriteLine("-----------------------------------");
  103.                 Console.WriteLine("....key(id):" + keyGroupSequence.Key);
  104.                 foreach (int options in keyGroupSequence)
  105.                 {
  106.                     Console.WriteLine("...option={0}", options);
  107.                 }
  108.             }
  109.         }
  110.         /*GroupBy第四种原型
  111.          * 
  112.          *
  113.          * 第四种具有第二种和第三中功能
  114.          * 
  115.          *    public static IEnumerable<IGrouping<K, E>> GroupBy<T, K, E>(
  116.          *       this IEnumerable<T> source,
  117.          *       Func<T, K> keySelector,
  118.          *       Func<T, E> elementSelector,
  119.          *       IEqualityComparer<K> comparer);
  120.          * 
  121.          */
  122.         static void GroupByFourthPrototype()
  123.         {
  124.             PresidentsOption[] presidentsOptions ={new PresidentsOption(1,10),
  125.                                                 new PresidentsOption(2,10),
  126.                                                 new PresidentsOption(2,50),
  127.                                                 new PresidentsOption(3,100),
  128.                                                 new PresidentsOption(4,30),
  129.                                                 new PresidentsOption(4,80),
  130.                                                 new PresidentsOption(5,200),
  131.                                                 new PresidentsOption(5,10),
  132.                                                 new PresidentsOption(6,90),
  133.                                                 new PresidentsOption(6,80),
  134.                                                 new PresidentsOption(7,10)};
  135.             GroupByCompare compare = new GroupByCompare();
  136.             IEnumerable<IGrouping<intint>> outerSequence = presidentsOptions.GroupBy(p => p.Id, p => p.Option,compare);
  137.             foreach (IGrouping<intint> keyGroupSequence in outerSequence)
  138.             {
  139.                 Console.WriteLine("-----------------------------------");
  140.                 Console.WriteLine("....key(hashCode):" + keyGroupSequence.Key);
  141.                 foreach (int options in keyGroupSequence)
  142.                 {
  143.                     Console.WriteLine("...option={0}", options);
  144.                 }
  145.             }
  146.         }
  147.         #endregion
用到的类:
  1.   public class PresidentsOption
  2.    {
  3.        public int Id
  4.        {
  5.            get;
  6.            set;
  7.        }
  8.        public int Option
  9.        {
  10.            get;
  11.            set;
  12.        }
  13.        public PresidentsOption(int id, int option)
  14.        {
  15.            this.Id = id;
  16.            this.Option = option;
  17.        }
  18.    }
  19.    //实现IEqualityComparer<T>
  20.    public class GroupByCompare : IEqualityComparer<int>
  21.    {
  22.        public bool Equals(int x, int y)
  23.        {
  24.            return (isLessFive(x) == isLessFive(y)); 
  25.        }
  26.        //小于返回1的HashCode、大于等于5的返回5的hashCode
  27.        public int GetHashCode(int x)
  28.        {
  29.            int intStart = 1;
  30.            int intCompare = 5;
  31.            return isLessFive(x) ? intStart.GetHashCode() : intCompare.GetHashCode();
  32.        }
  33.        public bool isLessFive(int x)
  34.        {
  35.            return (x < 5);
  36.        }
  37.    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值