LINQ to SQL查询中的通配符搜索未执行

时间:2017-01-19 14:09:21

标签: c# sql linq linq-to-sql

我正在尝试执行SQL" Like"使用带有以下查询的LINQ 2 SQL进行通配符搜索

{ _id: 5880c2562f109c2e17489155,
  password: '$2a$10$1TGM/Nnoii/ERt5YZFqaROJA0176bXw5wn7fF9B7.DrikVcW/Va4e',
  verified: false,
  __v: 0 }

然而,当我运行它时,在foreach行上我得到了

  

发生意外错误:LINQ to Entities无法识别   方法'布尔类似(System.String,System.String)'方法,这个   方法无法转换为商店表达式。

为什么将它视为LINQ to Entities查询?我认为这是纯粹的LINQ 2 SQL

2 个答案:

答案 0 :(得分:0)

做你想做的事情..我通常使用这个Utlis ..(通过通配符执行的扩展方法),....希望它能帮助你

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*","*")

答案 1 :(得分:0)

正如错误消息所示,它不是Linq to SQL,而是Linq to Entities,其中不支持SqlMethods类(see the Microsoft documentation)。我想,这是因为您针对实体框架的ObjectContext或DbContext运行查询。

在Linq to Entities上,以下内容将起作用:

var dbt = from th in _objectStoreRepository.GetHeaders()
          where th.CreatedBy.Contains("test")
          select th;