如何使用 LINQ、NHibernate 和 uNHAddins 进行分组和排序

发布于 2024-12-05 11:30:45 字数 1494 浏览 0 评论 0原文

我们正在构建一个 WPF 应用程序,使用 Oracle 数据库,还使用 ​​NHibernate 和 uNHAddins 扩展。在 DataGrid 中,我们尝试使用此查询 LINQ 从表中获取值:

return (from f in rFConsumption.GetAll()
                let d = f.CounterDtm
                where f.CounterDtm >= dataHoraInicio && f.CounterDtm <= dataHoraFim
                group f by (d.Year - 2000) * 384 + d.Month * 32 + d.Day into g
                select new RFConsumption
                {                       
                    COGCounter1 = (g.Sum(f => f.COGCounter1)),
                    BFCounter1 = (g.Sum(f => f.BFCounter1)),
                    NatGasCounter1 = (g.Sum(f => f.NatGasCounter1)),
                    MixGasCounter1 = (g.Sum(f => f.MixGasCounter1)),
                    COGCounter2 = (g.Sum(f => f.COGCounter2)),
                    BFCounter2 = (g.Sum(f => f.BFCounter2)),
                    NatGasCounter2 = (g.Sum(f => f.NatGasCounter2)),
                    MixGasCounter2 = (g.Sum(f => f.MixGasCounter2)),
                    COGCounter3 = (g.Sum(f => f.COGCounter3)),
                    BFCounter3 = (g.Sum(f => f.BFCounter3)),
                    NatGasCounter3 = (g.Sum(f => f.NatGasCounter3)),
                    MixGasCounter3 = (g.Sum(f => f.MixGasCounter3)),
                }
                ).ToList<RFConsumption>();

所以,我的问题是:

  • 如何使用 NHibernate 来使用 group by、order by;
  • 如何使 group by 将日期数据字段返回到指定的组。
  • We're building a WPF application, using Oracle database, also using NHibernate and uNHAddins extensions. In a DataGrid, we're trying to get values from a table, with this query LINQ:

    return (from f in rFConsumption.GetAll()
                    let d = f.CounterDtm
                    where f.CounterDtm >= dataHoraInicio && f.CounterDtm <= dataHoraFim
                    group f by (d.Year - 2000) * 384 + d.Month * 32 + d.Day into g
                    select new RFConsumption
                    {                       
                        COGCounter1 = (g.Sum(f => f.COGCounter1)),
                        BFCounter1 = (g.Sum(f => f.BFCounter1)),
                        NatGasCounter1 = (g.Sum(f => f.NatGasCounter1)),
                        MixGasCounter1 = (g.Sum(f => f.MixGasCounter1)),
                        COGCounter2 = (g.Sum(f => f.COGCounter2)),
                        BFCounter2 = (g.Sum(f => f.BFCounter2)),
                        NatGasCounter2 = (g.Sum(f => f.NatGasCounter2)),
                        MixGasCounter2 = (g.Sum(f => f.MixGasCounter2)),
                        COGCounter3 = (g.Sum(f => f.COGCounter3)),
                        BFCounter3 = (g.Sum(f => f.BFCounter3)),
                        NatGasCounter3 = (g.Sum(f => f.NatGasCounter3)),
                        MixGasCounter3 = (g.Sum(f => f.MixGasCounter3)),
                    }
                    ).ToList<RFConsumption>();
    

    So, my question is:

  • How I do to use group by, with order by, using NHibernate;

  • How can i make group by return date data field to that specified group.
  • 如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

    发布评论

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

    评论(1

    素衣风尘叹 2024-12-12 11:30:45

    您可以通过多种方式使用 NHibernate 编写相同的查询,对我来说最有趣的一种确实是 NHibernate QueryOver<>。
    因此,如果您的查询工作正常,那么此查询应该可以工作:

     return Session.QueryOver<rFConsumption>()
                .Where( fc => (fc.CounterDtm >= dataHoraInicio && fc.CounterDtm <= dataHoraFim))
                .SelectList(list => list
                    .SelectGroup(f => ((f.CounterDtm.Year - 2000) * 384 + f.CounterDtm.Month * 32 + f.CounterDtm.Day)) //use group by
                    .Select(exp =>
                        new RFConsumption()   //here you define the return data type based on your specified group
                        {
                            // exp[0] represents the data returned and grouped by the above statements, so here you can reform it to fit into the form of your new entity
                            // exp[0] here will be equivilant to g in your query
                        })
                        .OrderBy( ee => ee.COGCounter1 ) //order by any of the properties of RFConsumption
                        .ToList<RFConsumption>();
    

    您应该首先添加实体 RFConclusion:

    public calss RFConsumption
    {
         public int COGCounter1  { get; set; }
         public int BFCounter { get; set; } 
         ....
    }
    

    You can write the same query with NHibernate with several ways, the most interesting one for me really is the NHibernate QueryOver<>.
    So, if your query works fine then, this query should work:

     return Session.QueryOver<rFConsumption>()
                .Where( fc => (fc.CounterDtm >= dataHoraInicio && fc.CounterDtm <= dataHoraFim))
                .SelectList(list => list
                    .SelectGroup(f => ((f.CounterDtm.Year - 2000) * 384 + f.CounterDtm.Month * 32 + f.CounterDtm.Day)) //use group by
                    .Select(exp =>
                        new RFConsumption()   //here you define the return data type based on your specified group
                        {
                            // exp[0] represents the data returned and grouped by the above statements, so here you can reform it to fit into the form of your new entity
                            // exp[0] here will be equivilant to g in your query
                        })
                        .OrderBy( ee => ee.COGCounter1 ) //order by any of the properties of RFConsumption
                        .ToList<RFConsumption>();
    

    you should first add the entity RFConsumption:

    public calss RFConsumption
    {
         public int COGCounter1  { get; set; }
         public int BFCounter { get; set; } 
         ....
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文