Aliens 2018-10-19 13:59 采纳率: 100%
浏览 3046
已采纳

c# list如何实现动态分组?

目前我只会对列表的固定的多个字段进行分组,能否实现让用户选择多个字段进行分组?不要使用条件列举所有选择情况,字段数很多,组合出的数量也很大,没有操作性。
代码如下,RuleType, RuleLevel是两个分组字段,如何动态指定其他字段?使用表达式树构造lambda表达式是否可行?如何实现呢?谢谢!

StatisticList = DataList.GroupBy(t => new { t.RuleType, t.RuleLevel})
.Select(g => new Sta{
Type = g.Key.RuleType,
Level = g.Key.RuleLevel,
ScoreTop = g.Sum(t => t.ScoreTop),
}).ToList();

  • 写回答

2条回答

  • threenewbee 2018-10-19 17:21
    关注

    帮你写了一个
    图片说明

    完整代码如下

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Q703207
    {
        static class LinqExt
        {
            public class DGroupBy<T> : IGrouping<object[], T>
            {
                private List<T> _innerlist = new List<T>();
    
                private object[] _key;
    
                public DGroupBy(object[] key) { _key = key; }
    
                public object[] Key
                {
                    get { return _key; }
                }
    
                public void Add(T value)
                {
                    _innerlist.Add(value);
                }
    
                public IEnumerator<T> GetEnumerator()
                {
                    return this._innerlist.GetEnumerator();
                }
    
                System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
                {
                    return this._innerlist.GetEnumerator();
                }
            }
    
            public static IEnumerable<IGrouping<object[], T>> DynamicGroupBy<T>(this IEnumerable<T> data, string[] keys)
            {
                List<DGroupBy<T>> list = new List<DGroupBy<T>>();
                foreach (var item in data.Select(x => new { 
                    k = keys.Select(y => x.GetType().GetProperty(y).GetValue(x, null)).ToArray(),
                    v = x
                }))
                {
                    DGroupBy<T> existing = list.SingleOrDefault(x => x.Key.Zip(item.k, (a, b) => a.Equals(b)).All(y => y));
                    if (existing == null)
                    {
                        existing = new DGroupBy<T>(item.k);
                        list.Add(existing);
                    }
                    existing.Add(item.v);
                }
                return list;
            }
        }
    
        class User
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public string City { get; set; }
            public override string ToString()
            {
                return string.Format("{0},{1},{2},{3}", ID, Name, Age ,City);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                List<User> users = new List<User>()
                {
                    new User() { ID = 1, Age = 20, City = "BJ", Name = "A" },
                    new User() { ID = 2, Age = 20, City = "BJ", Name = "B" },
                    new User() { ID = 3, Age = 20, City = "BJ", Name = "C" },
                    new User() { ID = 4, Age = 20, City = "SH", Name = "D" },
                    new User() { ID = 5, Age = 30, City = "SH", Name = "E" },
                    new User() { ID = 6, Age = 30, City = "SH", Name = "F" },
                    new User() { ID = 7, Age = 30, City = "CD", Name = "G" },
                    new User() { ID = 8, Age = 30, City = "CD", Name = "H" },
                    new User() { ID = 9, Age = 30, City = "HK", Name = "I" },
                    new User() { ID = 10, Age = 30, City = "HK", Name = "J" },
                };
                var query = users.DynamicGroupBy(new string[] { "City" });
                foreach (var item in query)
                {
                    Console.WriteLine("Key: {0}", string.Join(",", item.Key.Select(x => x.ToString())));
                    foreach (var item1 in item)
                        Console.WriteLine("\t" + item1);
                }
            }
        }
    }
    
    

    因为csdn的女产品瞎搞,不得已,随便再问两个C#问题采纳我的回答,帮你用表达式树优化下代码(用反射比较慢)。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制