动态Linq Groupby SELECT键,列出<T>

ide*_*ain 4 c# linq

我正在使用Dynamic Linq helper来分组数据.我的代码如下:

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");
Run Code Online (Sandbox Code Playgroud)

如何从dynamiclinqquery中取回密钥和相应的分组项目列表,即IEnumerable {Key,List}?

ide*_*ain 8

我通过定义一个投影Key和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)
           {

           }                                          
       }
Run Code Online (Sandbox Code Playgroud)