通过用户定义的函数输出进行 Nhibernate 过滤 [英] Nhibernate filtering by user defined function output
问题描述
我是 NHibernate 的新手,到目前为止一切都进展顺利,但我遇到了一个问题,我不确定如何解决.基本上我需要通过用户定义函数的输出进行过滤.如果我用 SQL 写这就是我写的:
I'm reasonably new to NHibernate and everything has been going pretty well so far but I've come across a problem I'm not exactly sure of how to go about solving. Basically I need to filter by the output of a User Defined function. If I was writing in SQL this is what I'd write:
declare @Latitude decimal
declare @Longitude decimal
declare @radius int
set @Latitude = -118.4104684
set @Longitude = 34.1030032
select *
from store
where dbo.CalculateDistance([Latitude], [Longitude], @Latitude, @Longitude) < @radius
我已经看到了我认为不合适的公式属性、命名查询和创建您自己的方言扩展的示例(这似乎有点过头了).我本以为有更直接的方法可以做到,但我似乎找不到一个整洁的例子.
I've seen the formula attribute which I don't think is appropriate, named queries and examples of creating your own dialect extension (which seemed a little over kill). I would've thought there was a more straight forward way of doing it but I can't seem to find a tidy example.
推荐答案
您可以在休眠查询中使用 SQL 表达式.假设您已经映射了 Store
类型,您可以编写以下查询:
You could use SQL expression in your hibernate queries. Assuming you've mapped a Store
type you could write the following query:
var result = session
.CreateCriteria<Store>()
.Add(Expression.Sql(
"dbo.CalculateDistance({alias}.Latitude, {alias}.Longitude, ?, ?) < ?",
new object[] {
-118.4104684d,
34.1030032d,
100
},
new IType[] {
NHibernateUtil.Double,
NHibernateUtil.Double,
NHibernateUtil.Int32
}
))
.List<Store>();
这篇关于通过用户定义的函数输出进行 Nhibernate 过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!