LINQ中,表达式,NHibernate和同类比较 [英] Linq, Expressions, NHibernate and Like comparison
问题描述
我试图做决定比较类型(%字符串
或<$ C基于外部参数(通过搜索表单传递)LIKE比较$ C>字符串%或%字符串%
)
I am trying to do a like comparison based on an outside parameter (passed by a search form) that determines type of comparison ("%string"
or "string%"
or "%string%"
)
我想在以下方向:
query = query.Where(
Entity.StringProperty.Like("SearchString", SelectedComparsionType)
)
像法会比基于所选类型的返回
.StartsWith()
或 .EndsWith()
或 .SubString()
Like method would than based on selected type return
.StartsWith()
or .EndsWith()
or .SubString()
我表达的知识是从大远显然,因为我一直没能构建一个能产生正确的结果的方法(服务器端比较SQL就像对待 StartsWith
法)。
My knowledge of expressions is apparently far from great, since i haven't been able to construct a method that could yield the right result (server side comparison in SQL just like with StartsWith
method).
推荐答案
的简单的方法
只要使用
if (comparison == ComparisonType.StartsWith)
query = query.Where(e => e.StringProperty.StartsWith("SearchString"));
else if ...
难的方法
如果你想要做这样的事情,无论是请确保您的LINQ提供程序,可以说这种新方法的某种方式,以及它如何转换成SQL(不太可能),或阻止你的从曾经到达LINQ提供程序,并提供供应商的东西它理解(硬)方法。例如,而不是
If you want to do something like this, either make sure your LINQ provider can be told of this new method somehow, and how it translates to SQL (unlikely), or prevent your method from ever reaching the LINQ provider, and provide the provider something it understands (hard). For example, instead of
query.Where(e => CompMethod(e.StringProperty, "SearchString", comparsionType))
您可以创建类似
var query = source.WhereLike(e => e.StringProperty, "SearchString", comparsionType)
下面的代码
public enum ComparisonType { StartsWith, EndsWith, Contains }
public static class QueryableExtensions
{
public static IQueryable<T> WhereLike<T>(
this IQueryable<T> source,
Expression<Func<T, string>> field,
string value,
SelectedComparisonType comparisonType)
{
ParameterExpression p = field.Parameters[0];
return source.Where(
Expression.Lambda<Func<T, bool>>(
Expression.Call(
field.Body,
comparisonType.ToString(),
null,
Expression.Constant(value)),
p));
}
}
您甚至可以添加其他条件这样
You can even add additional criteria this way
var query = from e in source.WhereLike(
e => e.StringProperty, "SearchString", comparsionType)
where e.OtherProperty == 123
orderby e.StringProperty
select e;
的非常,非常艰难地
这将(在技术上)是可能的供应商看到它之前重写表达式树,所以你的可以的使用你脑子里在第一时间查询,但你' ð必须
It would (technically) be possible to rewrite the expression tree before the provider sees it, so you can use the query you had in mind in the first place, but you'd have to
- 创建一个
在哪里(这IQueryable的<的EntityType>源表达式来; Func键<的EntityType,布尔> >谓语)
拦截Queryable.Where
, - 重写表达式树,免去您
compMethod中
,无论它是用字符串
方法 - 通话之一原
Queryable.Where
通过改写的表达, - 和首先,可以按照上面的扩展方法在第一到位!
- create a
Where(this IQueryable<EntityType> source, Expression<Func<EntityType, bool>> predicate)
to intercept theQueryable.Where
, - rewrite the expression tree, replacing your
CompMethod
, wherever it is, with one of theString
methods, - call the original
Queryable.Where
with the rewritten expression, - and first of all, be able to follow the extension method above in the first place!
但是,这可能是你脑子里什么样的方式太复杂了。
But that's probably way too complicated for what you had in mind.
这篇关于LINQ中,表达式,NHibernate和同类比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!