动态Linq Groupby SELECT键,List& T& [英] Dynamic Linq Groupby SELECT Key, List<T>

查看:123
本文介绍了动态Linq Groupby SELECT键,List& T&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Dynamic 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");

如何从dynamiclinqquery中检索键和分组项目的相应列表,即IEnumerable {Key,List}?

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 SELECT键,List& T&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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