动态的LINQ GROUPBY选择键,显示列表< T> [英] Dynamic Linq Groupby SELECT Key, List<T>

查看:220
本文介绍了动态的LINQ GROUPBY选择键,显示列表< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LINQ的动态辅助进行分组的数据。我的代码如下:

I am using Dynamic Linq helper for grouping data. My code is as follows :

Employee[] empList = new Employee[6];
empList[0] = new Employee() { Name = "CA", State = "A", Department = "xyz" };
empList[1] = new Employee() { Name = "ZP", State = "B", Department = "xyz" };
empList[2] = new Employee() { Name = "AC", State = "B", Department = "xyz" };
empList[3] = new Employee() { Name = "AA", State = "A", Department = "xyz" };
empList[4] = new Employee() { Name = "A2", State = "A", Department = "pqr" };
empList[5] = new Employee() { Name = "BA", State = "B", Department = "pqr" };

var empqueryable = empList.AsQueryable();
var dynamiclinqquery  = DynamicQueryable.GroupBy(empqueryable, "new (State, Department)", "it");



我怎样才能拿回钥匙和分组的项目的相应列表IE的IEnumerable的{重点,列表}从dynamiclinqquery?

How can I get back the Key and corresponding list of grouped items i.e IEnumerable of {Key, List} from dynamiclinqquery ?

推荐答案

我通过定义项目的主要和员工列表中选择解决了这个问题。

I solved the problem by defining a selector that projects the Key as well as Employees List.

       var eq = empqueryable.GroupBy("new (State, Department)", "it").Select("new(it.Key as Key, it as Employees)");
       var keyEmplist = (from dynamic dat in eq select dat).ToList();

       foreach (var group in keyEmplist)
       {
           var key = group.Key;
           var elist = group.Employees;

           foreach (var emp in elist)
           {

           }                                          
       }

这篇关于动态的LINQ GROUPBY选择键,显示列表< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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