Linq 将 2 个键分组为一个
我为我的网站编写了一个简单的 OLAP 查看器。以下是类(抽象示例):
Employee
{
ID;
Name;
Roles[]; //What Employee can do
}
Order
{
Price;
Employee Manager;
Employee Executive; //Maybe wrong english. The person which perform order
}
员工可以同时是经理和执行人员。这意味着员工角色不是固定的。 我必须按员工对订单进行分组,最后使用员工密钥获得 IGrouping。
因此 .GroupBy(el=>new {el.Manager,el.Executive}) 是不允许的。
我考虑了 IEqualityComparable 的一些技巧,但没有找到解决方案。
如果有人能提供帮助,我会很高兴,谢谢。
I write a simple OLAP viewer for my web-site. Here are the classes (abstract example):
Employee
{
ID;
Name;
Roles[]; //What Employee can do
}
Order
{
Price;
Employee Manager;
Employee Executive; //Maybe wrong english. The person which perform order
}
Employee can be Manager and Executive in the order at the same time. This means that Employee role is not fixed.
I have to group orders by employees and finally get IGrouping with Employee key.
So .GroupBy(el=>new {el.Manager,el.Executive}) is not allowed.
I considered some tricks with IEqualityComparable, but found no solution.
If somrbody will help I'll be vary glad, thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个解决方案,假设您已经实现/覆盖了 gethashcode 和 equals
Here is a solution, with the assumption you have implemented/overridden gethashcode and equals
为了简化查看器的架构,我制作了转换函数,它为分析准备数据。稍后我会进行分组。这会产生副作用:订单的总和和计数发生变化,因为我们产生了一些新订单。但就我而言,由于架构的原因,这是最好的解决方案。如果我在使用数据库服务器时遇到问题,我将在此操作之前调用 ToList。我认为我的应用程序允许这种风格。再次感谢您! :)
To simplify architecture of the Viewer, I made transform function, which prepare data for analysis. I'll perform grouping later. This makes side effect: the sum and count of orders changes, because we produce some new orders. But in my case this is best solution, because of architecture. If I'll have troubles with DB Server, I'll invoke ToList Before this operation. I think this style is allowed by my app. Thank you one more time! :)