where子句中的扩展方法LINQ到实体 [英] Extension method in where clause in linq to Entities
问题描述
在LINQ到实体我们需要一个像工作的SQL像的方法。我们已经实现了我们自己的扩展方法来IQueryable的,因为contains方法为我们不行,因为不接受LIKE'%x%A%
In linq to Entities we needed a method that works like "sql like". We have implemented our own extension method to IQueryable, because contains method not work for us because doesn't accept patterns like '%a%b%'
创建的代码是
private const char WildcardCharacter = '%';
public static IQueryable<TSource> WhereLike<TSource>(this IQueryable<TSource> _source, Expression<Func<TSource, string>> _valueSelector, string _textSearch)
{
if (_valueSelector == null)
{
throw new ArgumentNullException("valueSelector");
}
return _source.Where(BuildLikeExpressionWithWildcards(_valueSelector, _textSearch));
}
private static Expression<Func<TSource, bool>> BuildLikeExpressionWithWildcards<TSource>(Expression<Func<TSource, string>> _valueSelector, string _textToSearch)
{
var method = GetPatIndexMethod();
var body = Expression.Call(method, Expression.Constant(WildcardCharacter + _textToSearch + WildcardCharacter), _valueSelector.Body);
var parameter = _valueSelector.Parameters.Single();
UnaryExpression expressionConvert = Expression.Convert(Expression.Constant(0), typeof(int?));
return Expression.Lambda<Func<TSource, bool>> (Expression.GreaterThan(body, expressionConvert), parameter);
}
private static MethodInfo GetPatIndexMethod()
{
var methodName = "PatIndex";
Type stringType = typeof(SqlFunctions);
return stringType.GetMethod(methodName);
}
这正常工作,并在SqlServer的完全执行的代码,但现在我们会里面使用where子句,因为这扩展方法:
This works correctly and the code is executed entirely in SqlServer, but now we would use this extension method inside where clause as:
myDBContext.MyObject.Where(o => o.Description.Like(patternToSearch) || o.Title.Like(patterToSerch));
的问题是,在where子句中使用的方法有,如果它是用来返回一个布尔结果像'||'经营者,我不知道该怎么做,我创建的代码返回一个布尔值,并保持在SQLSERVER执行的代码。我想,我必须BuildLinqExpression方法返回的表达式转换为bool,但我不知道该怎么做。
The problem is that the methods used in the where clause have to return a bool result if is it used with operators like '||' , and I don't know how to make that the code I have created returns a bool and keep the code executed in sqlserver. I suppose that I have to convert the returned Expression by BuildLinqExpression method to bool, but I don't know how to do it
要总结!首先,它可以创建Linq中我们自己的extensiond方式,以执行在SqlServer的代码实体?如果这是可能,我怎么也得办呢?
To sum up! First of all it's possible to create our own extensiond methods in Linq to Entities that execute the code in SqlServer? if this is possible how I have to do it?
感谢您的帮助。
推荐答案
没有,你不能教EF处理您的自定义扩展方法,即使你有构建一个的将的是可用来EF
No, you cannot educate EF to process your custom extension methods, even though you have code that builds expressions that would be usable to EF.
或者:
- 直接使用
SqlFunctions
方法,在你的EF其中,
表达式 - 使用
ExpressionVisitor
将多个表达式组合成复合(OrElse运算
/AndAlso运算
)的表达(注意这不会帮助你有这样的代码你想要的,但它会让你用你的两个方法并对其执行||
- 它看起来复杂,虽然)
- use the
SqlFunctions
methods directly in your EFWhere
expression - use an
ExpressionVisitor
to combine multiple expressions into a composite (OrElse
/AndAlso
) expression (note this won't help you have code like you want, but it will let you use your two methods and perform a||
on them - it will look complex, though)
首先是简单和清晰。
这篇关于where子句中的扩展方法LINQ到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!