Linq Dynamic按语句分组 [英] Linq Dynamic group by statement

查看:157
本文介绍了Linq Dynamic按语句分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个列(carType,颜色,所有者)的数据表.下面的查询是在2列(carType,colour)上分组.我的问题是如何通过使用反射使分组部分更具动态性.我的想法是将分组col添加到数组中,然后在查询分组部分使用反射.但是我被倒影吓了一跳.

I have datatable with 3 cols ( carType, colour, owner). Query below is grouping on 2 cols ( carType, colour).My question is how can i make the grouping portion more dynamic by using reflection. my idea is add the grouping col in to array, then use the reflection on the query grouping part. however i was struck at the reflection..

var gcols = new string[] { "CarType", "Colour" };
           var result = dt.AsEnumerable()
                                .GroupBy(x => new
                                {
                                    carType = x.Field<string>("carType"),
                                    colour = x.Field<string>("colour")
                                })
                                .Select(x => new
                                {
                                    CarType = x.Key.carType,
                                    Colour = x.Key.colour,
                                    count = x.Count()
                                })
                               .OrderBy(x => x.CarType).ToList();

推荐答案

var gcols = new string[] { "CarType", "Colour" };
         var result = dt.AsEnumerable()
                              .GroupBy(x => new[]
                              {
                                  x.Field<string>(gcols.FirstOrDefault()),
                                  x.Field<string>(gcols.LastOrDefault())
                              })
                              .Select(x => new
                              {
                                  CarType = x.Key.Where(p=>p.Contains(gcols.FirstOrDefault())),
                                  Colour = x.Key.Where(p=>p.Contains(gcols.LastOrDefault())),
                                  count = x.Count()
                              })
                             .OrderBy(x => x.CarType).ToList();


由AdaptiveLINQ(www. (adaptivelinq.com)旨在进行此类查询(多维分析查询).
它不使用动态LINQ,而是根据选择所表达的分组标准来转换查询..

例如,您可以编写:

按类型计数":
The .QueryByCube() function provided by AdaptiveLINQ (www.adaptivelinq.com) is designed to make this kind of query (multi-dimesensional analysis query).
It doesn''t use dynamic LINQ but transforms the query based on grouping criteria expressed by the selection.

For example, you can write:

"Count by type":
dt.QueryByCube(yourCubeDefinition)
  .Select(x => new {
    Type = x.Type,
    Count = c.Count
  })


按颜色计数":


"Count by color":

dt.QueryByCube(yourCubeDefinition)
  .Select(x => new {
    Color = x.Color,
    Count = c.Count
  })


按类型和颜色计数":


"Count by type and color":

dt.QueryByCube(yourCubeDefinition)
  .Select(x => new {
    Type = x.Type,
    Color = x.Color,
    Count = c.Count
  })


这篇关于Linq Dynamic按语句分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆