希望实现以下目标,但它失败了,因为locations.Any()
被视为IEnumerable
而不是IQueryable
,并且通过EF调用的标量函数需要IQueryable
。我需要这个过滤器在数据库级别发生(而不是首先实现列表)。
我怎样才能让locations.Any()
在这里被视为IQueryable
?我知道列表不存在于数据库中,但实体框架是否有办法理解这一点,并在SQL中嵌套OR
构建和AND
语句?
public Address GetAddresses(List<Loctions> locations)
{
_context.Addresses.
Where(a => locations.Any(l => MyContext.CustomFunction(l.PropA,l.PropB, a.PropA, a.ProbB) > 1 ))
}
[DbFunction("fn_DistanceBetweenCoordinates", "dbo")]
public static decimal CustomFunction(decimal SourceLatitude, decimal SourceLongitude, decimal TargetLatitude, decimal TargetLongitude) {
throw new NotImplementedException();
}
您可以通过将CustomFunction移到数据库中并在从EF查询时使用该服务器端函数来实现这一点。
请阅读用户定义的功能映射,并尝试根据您的用例调整示例。
我们还没有看到CustomFunction的主体,所以不可能判断从基于客户机的UDF到基于服务器的UDF的传输是否可行。
我们也不知道位置列表是如何填充的。这取决于如何完成,示例代码的改编可能会变得更加麻烦。