我在存储库中有一个现有的高级搜索方法,用于检查FormCollection
搜索条件是否存在,如果存在,则向搜索添加一个条件,例如
public IList<Residence> GetForAdvancedSearch(FormCollection collection)
{
var criteria = Session.CreateCriteria(typeof(Residence))
.SetResultTransformer(new DistinctRootEntityResultTransformer());
if (collection["MinBedrooms"] != null)
{
criteria
.Add(Restrictions.Ge("Bedrooms", int.Parse(collection["MinBedrooms"])));
}
// ... many criteria omitted for brevity
return criteria.List<Residence>();
}
我还进行了基本距离搜索,以查找每个住宅与搜索条件的距离。查询的 HBM 是
<sql-query name="Residence.Nearest">
<return alias="residence" class="Residences.Domain.Residence, Residences"/>
<return-scalar column="Distance" type="float"/>
SELECT R.*, dbo.GetDistance(:point, R.Coordinate) AS Distance
FROM Residence R
WHERE Distance < 10
ORDER BY Distance
</sql-query>
我必须定义一个函数来计算距离,因为没有办法让 NHibernate 转义 geography 函数中的冒号:
CREATE FUNCTION dbo.GetDistance
(
@firstPoint nvarchar(100),
@secondPoint GEOMETRY
)
RETURNS float
AS
BEGIN
RETURN GEOGRAPHY::STGeomFromText(
@firstPoint, 4326).STDistance(@secondPoint.STAsText()) / 1609.344
END
并且存储库因此调用命名查询:
return Session
.GetNamedQuery("Residence.Nearest")
.SetString("point", String.Format("POINT({0} {1})", latitude, longitude))
.List();
所以我的问题是;如何将两者结合起来(或从头开始),以便我可以过滤高级搜索结果以仅包含搜索位置 10 英里范围内的住宅?
更新我尝试使用 NHibernate.Spatial 和以下代码:
criteria.Add(SpatialExpression.IsWithinDistance(
"Coordinate", new Coordinate(latitude, longitude), 10));
但SpatialExpression.IsWithinDistance
返回了一个System.NotImplementedException
.