我有一个类似的课程
public class Empolyee
{
public string Designation {get ;set;}
public string Discipline {get ;set;}
public int Scale {get ;set;}
public DateTime DOB {get ;set;}
public int Sales {get ;set;}
}
Run Code Online (Sandbox Code Playgroud)
并以可数的说法记录所有员工的记录
List<Employee> Employees;
Run Code Online (Sandbox Code Playgroud)
和一个字符串键列表
var Keys = new List<string>()
{
"Designation",
"Scale",
"DOB"
};
Run Code Online (Sandbox Code Playgroud)
假设列表"Keys"的元素是用户指定的,用户可以指定no或许多关键元素.
现在我想使用列表"Keys"中指定的键对所有"Employees"进行分组,并仅选择"Keys"中指定的属性以及每个组的Sales of Sum.
在我尝试使用的3个解决方案中,以下看起来适用但无法使用它,因为不知道列表"Keys"将如何转换为匿名类型
Employees.GroupBy(e => new { e.Key1, e.Key2, ... })
.Select(group => new {
Key1 = group.Key.Key1,
Key2 = group.Key.Key2,
...
TotalSales = group.Select(employee => employee.Sales).Sum()
});
Run Code Online (Sandbox Code Playgroud)