Linq 将 2 个键分组为一个

发布于 2024-08-27 18:57:16 字数 491 浏览 5 评论 0原文

我为我的网站编写了一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-09-03 18:57:16

这是一个解决方案,假设您已经实现/覆盖了 gethashcode 和 equals

       var a = (
            from o in orders
            select new {
                Order = o,
                Empoyee = o.Manager
            }
            );


         var b = (
            from o in orders
            select new {
                Order = o,
                Empoyee = o.Executive
            }
            );

        var final = (a.Union(b)).Distinct().GroupBy(x=>x.Empoyee.ID, x=>x.Order);

        foreach (IGrouping<int, Order> o in final)
        {
            o.ToList();
        }

Here is a solution, with the assumption you have implemented/overridden gethashcode and equals

       var a = (
            from o in orders
            select new {
                Order = o,
                Empoyee = o.Manager
            }
            );


         var b = (
            from o in orders
            select new {
                Order = o,
                Empoyee = o.Executive
            }
            );

        var final = (a.Union(b)).Distinct().GroupBy(x=>x.Empoyee.ID, x=>x.Order);

        foreach (IGrouping<int, Order> o in final)
        {
            o.ToList();
        }
吻泪 2024-09-03 18:57:16

为了简化查看器的架构,我制作了转换函数,它为分析准备数据。稍后我会进行分组。这会产生副作用:订单的总和和计数发生变化,因为我们产生了一些新订单。但就我而言,由于架构的原因,这是最好的解决方案。如果我在使用数据库服务器时遇到问题,我将在此操作之前调用 ToList。我认为我的应用程序允许这种风格。再次感谢您! :)

public IQueryable TransForDim(IQueryable E)
        {
            if (this._DimType == Измерения.Сотрудники)
            {
                if (E.ElementType == typeof(Заказ))
                {
                    var Orders = E as IQueryable<Заказ>;
                    var a = (
                    from o in Orders
                    select new
                    {
                        Order = o,
                        Empoyee = o.Менеджер
                    }
                    );

                    var b = (
                       from o in Orders
                       select new
                       {
                           Order = o,
                           Empoyee = o.Исполнитель
                       }
                       );

                    var final = (a.Union(b)).Distinct().Select(e => new Заказ()
                    {
                        Менеджер = e.Empoyee,
                        Валюта = e.Order.Валюта,
                        Выполнено = e.Order.Выполнено,
                        Дата_Выполнения = e.Order.Дата_Выполнения,
                        Дата_Поступления = e.Order.Дата_Поступления,
                        Клиент = e.Order.Клиент,
                        Отменен = e.Order.Отменен,
                        Цена = e.Order.Цена
                    });
                    return final;
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Измерение не обрабатывает данные кроме заказов");
                }
            }
            else return E;
        }

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! :)

public IQueryable TransForDim(IQueryable E)
        {
            if (this._DimType == Измерения.Сотрудники)
            {
                if (E.ElementType == typeof(Заказ))
                {
                    var Orders = E as IQueryable<Заказ>;
                    var a = (
                    from o in Orders
                    select new
                    {
                        Order = o,
                        Empoyee = o.Менеджер
                    }
                    );

                    var b = (
                       from o in Orders
                       select new
                       {
                           Order = o,
                           Empoyee = o.Исполнитель
                       }
                       );

                    var final = (a.Union(b)).Distinct().Select(e => new Заказ()
                    {
                        Менеджер = e.Empoyee,
                        Валюта = e.Order.Валюта,
                        Выполнено = e.Order.Выполнено,
                        Дата_Выполнения = e.Order.Дата_Выполнения,
                        Дата_Поступления = e.Order.Дата_Поступления,
                        Клиент = e.Order.Клиент,
                        Отменен = e.Order.Отменен,
                        Цена = e.Order.Цена
                    });
                    return final;
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Измерение не обрабатывает данные кроме заказов");
                }
            }
            else return E;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文