LINQ To NHibernate忽略了'Group by'子句

时间:2011-08-05 14:10:53

标签: nhibernate linq-to-nhibernate

我使用NHibernate 3.2.0并且无法让LINQ提供程序为此语句生成正确的SQL查询:

var result = (from translation in session.Query<TmTranslation>()
              where translation.Id > 0
              group translation by translation.Language into grp
              select new { Lang = grp.Key.Code }).ToList();

生成的SQL是

select tmtranslat0_.id                        as id32_,
       tmtranslat0_.status                    as status32_,
       tmtranslat0_.text                      as text32_,
       tmtranslat0_.last_revision             as last4_32_,
       tmtranslat0_.fk_id_translation_unit    as fk5_32_,
       tmtranslat0_.fk_id_translator          as fk6_32_,
       tmtranslat0_.fk_id_last_modifier       as fk7_32_,
       tmtranslat0_.fk_id_last_match_category as fk8_32_,
       tmtranslat0_.fk_id_language            as fk9_32_
from   "TRANSLATION" tmtranslat0_
where  tmtranslat0_.id > 0

当然导致从数据库加载所有实体并将结果集分组到内存中(结果本身是正确的)。

我想要这样的东西

select tmtranslat0_.fk_id_language
from   "TRANSLATION" tmtranslat0_
where  tmtranslat0_.id > 0
group by tmtranslat0_.fk_id_language

代替生成。

我错过了什么吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

我唯一可以建议的是使用QueryOver API。