如何在EF中对数据进行两次分组?

发布于 2024-10-20 09:49:47 字数 1053 浏览 0 评论 0原文

这是我当前的 LINQ 查询:

            var list_1 = from z in _uow.Events.FindAll()
                     group z by new
                                    {
                                        z.GroupOfEvents.Customer,
                                        z.GroupOfEvents.GroupOfGroupOfEvents.Data.Year,
                                        z.GroupOfEvents.GroupOfGroupOfEvents.Data.Month,
                                        z.GroupOfEvents.GroupOfGroupOfEvents.Data.Day
                                    }
                         into zz
                         select new
                                    {
                                        zz.Key.Customer.Id,
                                        zz.Key.Year,
                                        zz.Key.Month,
                                        zz.Key.Day,
                                        Amount = zz.Sum(a => a.Amount)
                                    };

我想获取按客户 ID 分组的数据,然后按天(年月日)分组的数据。所以我想访问每个客户每天的特定“金额”。知道怎么做吗?

This is my current LINQ query:

            var list_1 = from z in _uow.Events.FindAll()
                     group z by new
                                    {
                                        z.GroupOfEvents.Customer,
                                        z.GroupOfEvents.GroupOfGroupOfEvents.Data.Year,
                                        z.GroupOfEvents.GroupOfGroupOfEvents.Data.Month,
                                        z.GroupOfEvents.GroupOfGroupOfEvents.Data.Day
                                    }
                         into zz
                         select new
                                    {
                                        zz.Key.Customer.Id,
                                        zz.Key.Year,
                                        zz.Key.Month,
                                        zz.Key.Day,
                                        Amount = zz.Sum(a => a.Amount)
                                    };

I would like to get data grouped by customer id, and then by day (year-month-day). So I want to access particular "Amount" per customer per day. Any idea how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

没有伤那来痛 2024-10-27 09:49:47

到目前为止,我已经做到了,但我不确定这是否是最好的方法?

            var list_a =
            from klient in _uow.Klient.FindAll()
            select
                new
                    {
                        Klient = klient,
                        Days =
                from grupaZdarzen in klient.GrupaZdarzen
                group grupaZdarzen by new
                                          {
                                              grupaZdarzen.ZestawGrupZdarzen.Data.Year,
                                              grupaZdarzen.ZestawGrupZdarzen.Data.Month,
                                              grupaZdarzen.ZestawGrupZdarzen.Data.Day
                                          }
                into grupyZdarzenByDay
                select new
                           {
                               Data = grupyZdarzenByDay.Key,
                               Total =
                    (from grupaZdarzenByDay in grupyZdarzenByDay
                     from zdarzeniaByGrupa in grupaZdarzenByDay.Zdarzenie
                     select zdarzeniaByGrupa.Ilosc).Sum()
                           }
                    };

        var list_b = list_a.ToDictionary(a => a.Klient.Id,
                                         a =>
                                         a.Days.ToDictionary(
                                             b => b.Data.Day + "/" + b.Data.Month + "/" + b.Data.Year, b => b.Total));

So far I came with this, but I'm not sure if this is the best way?

            var list_a =
            from klient in _uow.Klient.FindAll()
            select
                new
                    {
                        Klient = klient,
                        Days =
                from grupaZdarzen in klient.GrupaZdarzen
                group grupaZdarzen by new
                                          {
                                              grupaZdarzen.ZestawGrupZdarzen.Data.Year,
                                              grupaZdarzen.ZestawGrupZdarzen.Data.Month,
                                              grupaZdarzen.ZestawGrupZdarzen.Data.Day
                                          }
                into grupyZdarzenByDay
                select new
                           {
                               Data = grupyZdarzenByDay.Key,
                               Total =
                    (from grupaZdarzenByDay in grupyZdarzenByDay
                     from zdarzeniaByGrupa in grupaZdarzenByDay.Zdarzenie
                     select zdarzeniaByGrupa.Ilosc).Sum()
                           }
                    };

        var list_b = list_a.ToDictionary(a => a.Klient.Id,
                                         a =>
                                         a.Days.ToDictionary(
                                             b => b.Data.Day + "/" + b.Data.Month + "/" + b.Data.Year, b => b.Total));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文