用户定义函数输出进行的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屋!