首页 新闻 会员 周边

关于group by多字段分组动态表达式生成

0
悬赏园豆:100 [已解决问题] 解决于 2017-03-01 19:59

需要试下一个list动态分组功能,分组字段是多个并且是不确定的,求表达式生成方式。代码如下

1.实体定义

    public class House
    {
        public string Name { get; set; }

        public string Month { get; set; }

        public int DfFee { get; set; }

        public int SfFee { get; set; }
    }

2.单条件动态分组

复制代码
 List<House> list = new List<House>();
            list.Add(new House
            {
                Name = "张三",
                Month = "2017-01",
                DfFee = 100,
                SfFee = 20
            });

            list.Add(new House
            {
                Name = "张三",
                Month = "2017-02",
                DfFee = 20,
                SfFee = 100
            });

            list.Add(new House
            {
                Name = "张三",
                Month = "2017-03",
                DfFee = 10,
                SfFee = 40
            });

            list.Add(new House
            {
                Name = "李四",
                Month = "2017-01",
                DfFee = 67,
                SfFee = 24
            });

            list.Add(new House
            {
                Name = "李四",
                Month = "2017-04",
                DfFee = 672,
                SfFee = 243
            });

            var type = typeof(House);
            var parameter = Expression.Parameter(type, "e");
            var propertyReference = Expression.Property(parameter,
                    "Name");//根据Name分组
            var func = Expression.Lambda<Func<House, dynamic>>
                    (propertyReference , new[] { parameter }).Compile();

            foreach (var item in list.GroupBy(func))
            {
                Console.WriteLine(item.Key);
            }
复制代码

  上述代码可以根据House的任何属性进行分组

 

3.现在我想生成 list.GroupBy(e => new { e.Name, e.Month });动态表达式,请问该怎么写

焰尾迭的主页 焰尾迭 | 初学一级 | 园豆:33
提问于:2017-02-22 22:40
< >
分享
最佳答案
0

Dynamic linq

https://dynamiclinq.codeplex.com/SourceControl/latest#DynamicLinq/System.Linq.Dynamic/Dynamic.cs

 

 

            var query = list.AsQueryable()
                .GroupBy(string.Format("new ({0}, {1})", "Name", "Month"), "it")
                .Select("new(it.Key as Key, it as Houses)");
            var groupList = (from dynamic g in query select g).ToList();

            foreach (var g in groupList)
            {
                g.Key.Name, g.Key.Month;
                foreach (var h in g.Houses)
                  //     h.Name

 

收获园豆:100
Qlin | 老鸟四级 |园豆:2403 | 2017-02-23 09:20
其他回答(2)
-1

 list.GroupBy(string.Format("new ({0})", string.Join(",", allDimentsion)), "new(it as Vm)") as IEnumerable<IGrouping<dynamic, dynamic>>;

焰尾迭 | 园豆:33 (初学一级) | 2017-03-01 19:58
0

你好,我想问一下 list.GroupBy(string.Format("new ({0})", string.Join(",", allDimentsion)), "new(it as Vm)") as IEnumerable<IGrouping<dynamic, dynamic>>;

红色字体部分中的it和Vm是什么意思,为什么我用的时候提示是这样的?

闲得无聊敲代码 | 园豆:159 (初学一级) | 2017-05-09 17:24

参照 http://www.cnblogs.com/yanweidie/p/6485957.html

支持(0) 反对(0) 焰尾迭 | 园豆:33 (初学一级) | 2017-05-09 17:25

请参照这个文章,有详细介绍

List,DataTable实现行转列的通用方案

支持(0) 反对(0) 焰尾迭 | 园豆:33 (初学一级) | 2017-05-09 17:27

@焰尾迭: O(∩_∩)O谢谢

支持(0) 反对(0) 闲得无聊敲代码 | 园豆:159 (初学一级) | 2017-05-09 17:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册