zoukankan      html  css  js  c++  java
  • 非常好用的一个分组扩展方法

     public static class GroupHelper
        {
            public static List<List<object>> Group(this List<object> source, Func<List<object>, bool> limitFunc)
            {
                return Group<object>(source, limitFunc);
            }
    
            public class t {
                public string key { get; set; }
            }
            public static object test()
            {
                var list = new List<t> { 
                    new t { key = "b" }, new t { key = "c" },new t { key = "b" },
                    new t { key = "a" },new t { key = "c" },new t { key = "e6" }, new t { key = "e9" }, new t { key = "c" },
                    new t { key = "e1" }, new t { key = "e4" },new t { key = "e7" },new t { key = "e10" },
                     new t { key = "b" },new t { key = "e5" },new t { key = "e8" },new t { key = "e11" },
                    new t { key = "e3" },  new t { key = "e13" },new t { key = "a" },new t { key = "e2" },new t { key = "e12" },
                };
                var res = list.GroupBy(m => m.key).OrderByDescending(m => m.Count()).SelectMany(m => m).ToList();
                return res.Select(m=>m.key) ;
                //"b", "b", "b", "c", "c", "c", "a", "a", "e6", "e9", "e1", "e4", "e7", "e10", "e5", "e8", "e11", "e3", "e13", "e2", "e12"
                var source = "a a b b b c c c d d e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 e12 e13".Split(' ').ToList();
                var groups = source.Group(r => (!(r.Count == 2 && r.Distinct().Count() == 2) && r.Count > 1 && r.Distinct().Count() == 2) || (r.Count == 13 && r.Distinct().Count() == 13));
                return groups;
                //result:
                //aa
                //bbb
                //ccc
                //dd
                //e1-e12
                //e13
            }
            /// <summary>
            /// 分组
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <param name="limitFunc">项限制</param>
            /// <returns></returns>
            public static List<List<T>> Group<T>(this List<T> source, Func<List<T>, bool> limitFunc)
            {
                var result = new List<List<T>>();
    
                List<T> resItem = null;
                var inter = source.GetEnumerator();
                foreach (var item in source)
                {
                    if (resItem == null) resItem = new List<T>();
    
                    resItem.Add(item);
                    //超限移除当前项并移交 result,创建新项。
                    if (limitFunc(resItem.ToList()))
                    {
                        resItem.Remove(item);
                        result.Add(resItem);
                        resItem = new List<T> { item };
                    }
                }
                if (resItem != null && resItem.Count > 0)
                {
                    result.Add(resItem);
                }
    
                return result;
            }
    }
  • 相关阅读:
    ural 1080 Map Coloring DFS染色
    hdu 4287 Intelligent IME
    hdu 4268 Alice and Bob 区域赛 1002 (STL、SBT实现)
    SBT专题训练
    hdu 4276 The Ghost Blows Light 区域网络赛 1010 树上背包+spfa
    hdu 4278 Faulty Odometer
    hdu 4279 Number
    VIM 插件(转)
    Linux环境变量的设置(转)
    福昕PDF阅读器 v3.3 破解
  • 原文地址:https://www.cnblogs.com/zhuwansu/p/10249150.html
Copyright © 2011-2022 走看看