gpt4 book ai didi

c# - 动态 Linq Groupby SELECT Key, List

转载 作者:太空狗 更新时间:2023-10-30 00:33:20 24 4
gpt4 key购买 nike

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

我怎样才能从 dynamiclinqquery 取回 key 和相应的分组项目列表,即 {Key, List} 的 IEnumerable?

最佳答案

我通过定义一个投影键和员工列表的选择器解决了这个问题。

       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)
{

}
}

关于c# - 动态 Linq Groupby SELECT Key, List<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12396813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com