如何按LINQ,NHibernate和uNHAddins进行分组和排序

时间:2011-09-20 19:55:26

标签: wpf linq nhibernate unhaddins

我们正在使用Oracle数据库构建WPF应用程序,同时使用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;
  • 如何通过返回日期数据字段将组创建到指定的组。
  • 1 个答案:

    答案 0 :(得分:1)

    您可以使用NHibernate以多种方式编写相同的查询,对我来说最有趣的是NHibernate QueryOver&lt;&gt;。 因此,如果您的查询工作正常,则此查询应该有效:

     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>();
    

    您应首先添加实体RFConsumption:

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