Dynamic Linq Groupby SELECT Key, List

后端 未结 2 1136
迷失自我
迷失自我 2021-01-13 18:33

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\",          


        
相关标签:
2条回答
  • 2021-01-13 18:46

    The GroupBy method should still return something that implements IEnumerable<IGrouping<TKey, TElement>>.

    While you might not be able to actually cast it (I'm assuming it's dynamic), you can certainly still make calls on it, like so:

    foreach (var group in dynamiclinqquery)
    {
        // Print out the key.
        Console.WriteLine("Key: {0}", group.Key);
    
        // Write the items.
        foreach (var item in group)
        {
            Console.WriteLine("Item: {0}", item);
        }
    }
    
    0 讨论(0)
  • 2021-01-13 19:05

    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)
               {
    
               }                                          
           }
    
    0 讨论(0)
提交回复
热议问题