Nhibernate 的聚合函数

今天遇到的问题:

Hql="select max(SNInfo.Ln) from SNInfo",设置MaxResult(1),执行这条查询时,
返回的是SNInfo对象,其中LN是519,query的返回结果集也不是1。
改为
IQuery q = _coreRepository.ActiveSession.CreateQuery("select max(s.Ln) from SNInfo as s"  );
q.UniqueResult();
发现,不能用类名修饰字段,必须用别名。
查找NHibernate源代码:

None.gif if ( (dialect  is  Dialect.MsSql2000Dialect) ) 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif     Assert.AreEqual( 
1, s.Find("from s in class Simple where lower( s.Name + ' foo' ) = 'simple 1 foo'").Count );
ExpandedBlockEnd.gif    }

None.gif
None.gif if ( (dialect  is  Dialect.SybaseDialect) ) 
ExpandedBlockStart.gifContractedBlock.gif                
dot.gif {
InBlock.gif                    Assert.AreEqual( 
1, s.Find("from s in class Simple where lower( concat(s.Name, ' foo') ) = 'simple 1 foo'").Count );
ExpandedBlockEnd.gif                }
疑问:
"in class" 和 as的功能相似?
某些函数,如lower在不同的数据库中语法不同,在NHibernate中用发也不同,这样还怎么跨数据库?
None.gif enumerable  =  s.Enumerable(
None.gif                
" select max(foo.Component.ImportantDates.elements) from foo in class Foo group by foo.id "
None.gif                );
None.gif            IEnumerator enumerator 
=  enumerable.GetEnumerator();
None.gif
None.gif            Assert.IsTrue( enumerator.MoveNext() );
None.gif            Assert.IsTrue( enumerator.Current 
is  DateTime );
可以同过IEnumerator 获取数据,且效率比find()高
Entities returned as results are initialized on demand. The first SQL query returns
 identifiers only. So Enumerator()is usually a less efficient way to retrieve
object than Find()
调用例子:
None.gif enumerable  =  s.Enumerable(  " select count(foo) from foo in class Foo where foo.id=? " ,
None.gif                                       foo.Key, NHibernateUtil.String );
<param name="query">The query string</param>
第二个参数:<param name="value">A value to be written to a "?" placeholder in the query string</param>代替?占位符的值
第三个:<param name="type">The hibernate type of the value</param>
后面两个参数会组合成QueryParameters定义如下:
public QueryParameters( IType[ ] positionalParameterTypes, object[ ] positionalParameterValues )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值