gpt4 book ai didi

entity-framework - Entity Framework 中的 "like"查询

转载 作者:行者123 更新时间:2023-12-03 18:08:04 26 4
gpt4 key购买 nike

如何使用 edo Entity Framework 在 ASP.net MVC 中获取通配 rune 本搜索(如 SQL 的“like”语句)?

我认为这会起作用:

var elig = (from e in _documentDataModel.Protocol_Eligibility_View
where e.criteria.Contains(query)
select e);

但即使在搜索数据库中肯定存在的查询字符串时,它也不会返回任何结果。我究竟做错了什么?

最佳答案

这家伙为 Linq 制作了一个非常好的“WhereLike”扩展,它接受任何通配符并将两个值(其中一个来自表达式)与从通配符位置派生的通用方法进行比较。

  • x% -> 开始于
  • %x -> 以
  • 结尾
  • %x% -> 包含

  • http://trentacular.com/2010/08/linq-to-entities-wild-card-like-extension-method/

    编辑:
    文章好像挂了。我将在下面粘贴扩展代码:
    public static class LinqHelper
    {
    //Support IQueryable (Linq to Entities)
    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));
    }

    //Support IEnumerable (Linq to objects)
    public static IEnumerable<TSource> WhereLike<TSource>(this IEnumerable<TSource> sequence, Func<TSource, string> expression, string value, char wildcard)
    {
    var regEx = WildcardToRegex(value, wildcard);

    //Prevent multiple enumeration:
    var arraySequence = sequence as TSource[] ?? sequence.ToArray();

    try
    {
    return arraySequence.Where(item => Regex.IsMatch(expression(item), regEx));
    }
    catch (ArgumentNullException)
    {
    return arraySequence;
    }
    }

    //Used for the IEnumerable support
    private static string WildcardToRegex(string value, char wildcard)
    {
    return "(?i:^" + Regex.Escape(value).Replace("\\" + wildcard, "." + wildcard) + "$)";
    }

    //Used for the IQueryable support
    private 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);
    }

    private static MethodInfo GetLikeMethod(string value, char wildcard)
    {
    var methodName = "Equals";

    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";
    }

    var stringType = typeof(string);
    return stringType.GetMethod(methodName, new[] { stringType });
    }
    }
    }

    关于entity-framework - Entity Framework 中的 "like"查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2542288/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com