2011-09-20 60 views
2

我们正在使用Oracle数据库构建WPF应用程序,同时使用NHibernate和uNHAddins扩展。在DataGrid中,我们试图从表中获取的值,这个查询LINQ:如何使用LINQ,NHibernate和uNHAddins进行分组和排序

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 ;
  • 如何使返回日期数据字段组指向该指定的组。
  • 回答

    1

    你可以用几种方法用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>(); 
    

    你应该先添加实体RFConsumption:

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

    感谢您的回答,但我忘了告诉的东西。我们使用MVVM,而NHibernate则使用了Layer Service。 这是rFConsumption.GetAll()运行: 公众的IQueryable GETALL(){ 回报 (从吨Session.Query () 选择T); } 我该如何修改? 再次感谢。 –

    +0

    好的,我已经更改了服务层并实现了IQueryOver Find()。现在,我的查询是: ---- return(from f in rFConsumption.Find() where(fc =>((fc.CounterDtm> = dataHoraInicio)&&(fc.CounterDtm <= dataHoraFim))。SelectList( list => list).SelectGroup(g =>((g.CounterDtm.Year - 2000)* 384 + g.CounterDtm.Month * 32 + g.CounterDtm.Day).Select(exp => new RFConsumption() { ... }) .OrderBy(EE => ee.COGCounter1)) .ToList ()); - 错误: 成FC - >无法转换lambda表达式为类型 '布尔',因为它不是代表类型 –

    +0

    我可以做什么? –