[英]Wildcard search in LINQ to SQL query not executing
我正在嘗試使用帶有以下查詢的LINQ 2 SQL執行SQL“ Like”通配符搜索
public IQueryable<DbHeader> GetHeaders()
{
return Work.Context.Headers
.Include(m => m.Terms.Select(t => t.MetaDataPairs))
.Include(m => m.States)
.Include(m => m.Attachments);
}
var dbt = from th in _objectStoreRepository.GetHeaders()
where SqlMethods.Like(th.CreatedBy, "test")
select th;
foreach (var t in dbt) {
...
但是,當我運行它時,在foreach行上我得到
發生意外錯誤:LINQ to Entities無法識別方法'Boolean Like(System.String,System.String)'方法,並且該方法無法轉換為商店表達式。
為什么考慮將它視為LINQ to Entities查詢? 我以為這是純LINQ 2 SQL
做你想做的..我通常使用這個Utlis ..(擴展方法,用通配符執行wherelike),....希望它能幫助你
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Fwd.Repository.EF.Utils
{
public static class LinqUtils
{
/// <summary>
/// Wheres the like.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <param name="source">The source.</param>
/// <param name="valueSelector">The value selector.</param>
/// <param name="value">The value.</param>
/// <param name="wildcard">The wildcard.</param>
/// <returns></returns>
public static IQueryable<TSource> WhereLike<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, string>> valueSelector, string value, char wildcard)
{
return source.Where(BuildLikeExpression(valueSelector, value, wildcard));
}
/// <summary>
/// Builds the like expression.
/// </summary>
/// <typeparam name="TElement">The type of the element.</typeparam>
/// <param name="valueSelector">The value selector.</param>
/// <param name="value">The value.</param>
/// <param name="wildcard">The wildcard.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">valueSelector</exception>
public static Expression<Func<TElement, bool>> BuildLikeExpression<TElement>(
Expression<Func<TElement, string>> valueSelector, string value, char wildcard)
{
if (valueSelector == null)
throw new ArgumentNullException("valueSelector");
var method = GetLikeMethod(value, wildcard);
value = value.Trim(wildcard);
var body = Expression.Call(valueSelector.Body, method, Expression.Constant(value));
var parameter = valueSelector.Parameters.Single();
return Expression.Lambda<Func<TElement, bool>>(body, parameter);
}
/// <summary>
/// Gets the like method.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="wildcard">The wildcard.</param>
/// <returns></returns>
private static MethodInfo GetLikeMethod(string value, char wildcard)
{
var methodName = "Contains";
var textLength = value.Length;
value = value.TrimEnd(wildcard);
if (textLength > value.Length)
{
methodName = "StartsWith";
textLength = value.Length;
}
value = value.TrimStart(wildcard);
if (textLength > value.Length)
{
methodName = (methodName == "StartsWith") ? "Contains" : "EndsWith";
textLength = value.Length;
}
var stringType = typeof(string);
return stringType.GetMethod(methodName, new Type[] { stringType });
}
}
}
然后您可以在IQueryable實體上使用它,例如
var query = _dbContext.Users.WhereLike(xx=>xx.name,"fred*","*")
如錯誤消息所述,不是Linq to SQL,而是Linq to Entities,其中不支持SqlMethods類( 請參閱Microsoft文檔 )。 我猜是因為您針對實體框架的ObjectContext或DbContext運行查詢。
在Linq to Entities上,以下方法將起作用:
var dbt = from th in _objectStoreRepository.GetHeaders()
where th.CreatedBy.Contains("test")
select th;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.