Tsql Linq到NHibernate Plus sql用户定义函数

Tsql Linq到NHibernate Plus sql用户定义函数,tsql,linq-to-nhibernate,user-defined-functions,Tsql,Linq To Nhibernate,User Defined Functions,我有一个相当大的linq到nhibernate查询。我现在需要添加一个过滤器,它基于一个用t-sql编写的用户定义函数,我必须将参数传递到该函数中。在我的例子中,我需要传入一个用户键入的zipcode,并将其传递给t-sql函数,以便根据与此zip的距离进行过滤。这是可能的,还是我需要使用ICriteria api重写我的查询?我确实找到了解决方案: 请注意具有RegisterCustomAction的NHibernate查询(nquery): private void CallZipSqlFu

我有一个相当大的linq到nhibernate查询。我现在需要添加一个过滤器,它基于一个用t-sql编写的用户定义函数,我必须将参数传递到该函数中。在我的例子中,我需要传入一个用户键入的zipcode,并将其传递给t-sql函数,以便根据与此zip的距离进行过滤。这是可能的,还是我需要使用ICriteria api重写我的查询?

我确实找到了解决方案:

请注意具有RegisterCustomAction的NHibernate查询(nquery):

private void CallZipSqlFunction(ListingQuerySpec spec, IQueryable<Listing> query)
        {

            var nQuery = query as NHibernate.Linq.Query<Listing>;

            //todo: find some way to paramaterize this or use linq-to-nh and not criteria to call the function
            // so i don thave to check for escape chars in zipcode
            if (spec.ZipCode.Contains("'"))
                throw new SecurityException("invalid character");

            //yuck!
            var functionString = "dbo.GetDistanceForListing('" + spec.ZipCode + "',{alias}.ID) as Distance";
            //create a projection representing the function call
            var distance = Projections.SqlProjection(functionString, new[] { "Distance" }, new IType[] { NHibernateUtil.String });

            //create a filter based on the projection
            var filter = Expression.Le(distance, spec.ZipCodeRadius.Value);

            //add the distance projection as order by
            nQuery.QueryOptions.RegisterCustomAction(x => x.AddOrder(Order.Asc(distance)));

            //add teh distance filter
            nQuery.QueryOptions.RegisterCustomAction(x => x.Add(filter));
        }
private void CallZipSqlFunction(ListingQuerySpec规范,IQueryable查询)
{
var nQuery=查询为NHibernate.Linq.query;
//todo:找到一些方法对此进行参数化,或者使用linq to nh而不是条件来调用函数
//所以我不必检查zipcode中的逃逸字符
if(spec.ZipCode.Contains(“'))
抛出新的SecurityException(“无效字符”);
//恶心!
var functionString=“dbo.getDistanceForList(“+spec.ZipCode+”,{alias}.ID)作为距离”;
//创建表示函数调用的投影
var distance=Projections.SqlProjection(functionString,new[]{“distance”},new IType[]{NHibernateUtil.String});
//基于投影创建过滤器
var filter=Expression.Le(距离,spec.ZipCodeRadius.Value);
//按顺序添加距离投影
RegisterCustomAction(x=>x.AddOrder(Order.Asc(distance));
//添加距离过滤器
RegisterCustomAction(x=>x.Add(filter));
}

我也有同样的问题。您是否提出了解决方案或使用ICriteria api重写了查询?