代码之家  ›  专栏  ›  技术社区  ›  brien

像实体框架中的运算符一样?

  •  116
  • brien  · 技术社区  · 16 年前

    blog post

    9 回复  |  直到 16 年前
        1
  •  28
  •   Yann Duran    7 年前

    where entity.Name.Contains("xyz")
    

    WHERE Name LIKE '%xyz%'
    

    StartsWith EndsWith

    我不完全确定这是否有帮助,因为当你说你试图这样做时,我不明白你的意思

        2
  •  142
  •   Jon Skeet    16 年前

    Where(obj => DbFunctions.Like(obj.Column , "%expression%"))
    
        3
  •  32
  •   surfen    12 年前

    LIKE Entity Framework Core 2.0 :

    var query = from e in _context.Employees
                        where EF.Functions.Like(e.Title, "%developer%")
                        select e;
    

    ... where e.Title.Contains("developer") ... SQL 喜欢 CHARINDEX 我们看到 Contains 方法。

        4
  •  14
  •   Lode Vlaeminck    8 年前

    这是一篇旧文章,但对于任何寻找答案的人来说, this link this answer this answer

    简短版本:

    方法-在所有有效的文本和字符数据类型上,返回指定表达式中模式第一次出现的起始位置,如果找不到模式,则返回零

    命名空间:系统。数据。物体。SqlClient

    这里也有一些解释 forum thread .

        6
  •  5
  •   Robert Harvey    16 年前

    文档中特别提到它是Entity SQL的一部分。您是否收到错误消息?

    // LIKE and ESCAPE
    // If an AdventureWorksEntities.Product contained a Name 
    // with the value 'Down_Tube', the following query would find that 
    // value.
    Select value P.Name FROM AdventureWorksEntities.Product 
        as P where P.Name LIKE 'DownA_%' ESCAPE 'A'
    
    // LIKE
    Select value P.Name FROM AdventureWorksEntities.Product 
        as P where P.Name like 'BB%'
    

    http://msdn.microsoft.com/en-us/library/bb399359.aspx

        7
  •  2
  •   teamchong    11 年前

    protected override Expression<Func<YourEntiry, bool>> BuildLikeExpression(string searchText)
        {
            var likeSearch = $"%{searchText}%";
    
            return t => EF.Functions.Like(t.Code, likeSearch)
                        || EF.Functions.Like(t.FirstName, likeSearch)
                        || EF.Functions.Like(t.LastName, likeSearch);
        }
    
    //Calling method
    
    var query = dbContext.Set<YourEntity>().Where(BuildLikeExpression("Text"));
    
        8
  •  0
  •   th2tran    16 年前

    public static class ExpressionExtension
    {
        public static Expression<Func<T, bool>> Like<T>(Expression<Func<T, string>> expr, string likeValue)
        {
            var paramExpr = expr.Parameters.First();
            var memExpr = expr.Body;
    
            if (likeValue == null || likeValue.Contains('%') != true)
            {
                Expression<Func<string>> valExpr = () => likeValue;
                var eqExpr = Expression.Equal(memExpr, valExpr.Body);
                return Expression.Lambda<Func<T, bool>>(eqExpr, paramExpr);
            }
    
            if (likeValue.Replace("%", string.Empty).Length == 0)
            {
                return PredicateBuilder.True<T>();
            }
    
            likeValue = Regex.Replace(likeValue, "%+", "%");
    
            if (likeValue.Length > 2 && likeValue.Substring(1, likeValue.Length - 2).Contains('%'))
            {
                likeValue = likeValue.Replace("[", "[[]").Replace("_", "[_]");
                Expression<Func<string>> valExpr = () => likeValue;
                var patExpr = Expression.Call(typeof(SqlFunctions).GetMethod("PatIndex",
                    new[] { typeof(string), typeof(string) }), valExpr.Body, memExpr);
                var neExpr = Expression.NotEqual(patExpr, Expression.Convert(Expression.Constant(0), typeof(int?)));
                return Expression.Lambda<Func<T, bool>>(neExpr, paramExpr);
            }
    
            if (likeValue.StartsWith("%"))
            {
                if (likeValue.EndsWith("%") == true)
                {
                    likeValue = likeValue.Substring(1, likeValue.Length - 2);
                    Expression<Func<string>> valExpr = () => likeValue;
                    var containsExpr = Expression.Call(memExpr, typeof(String).GetMethod("Contains",
                        new[] { typeof(string) }), valExpr.Body);
                    return Expression.Lambda<Func<T, bool>>(containsExpr, paramExpr);
                }
                else
                {
                    likeValue = likeValue.Substring(1);
                    Expression<Func<string>> valExpr = () => likeValue;
                    var endsExpr = Expression.Call(memExpr, typeof(String).GetMethod("EndsWith",
                        new[] { typeof(string) }), valExpr.Body);
                    return Expression.Lambda<Func<T, bool>>(endsExpr, paramExpr);
                }
            }
            else
            {
                likeValue = likeValue.Remove(likeValue.Length - 1);
                Expression<Func<string>> valExpr = () => likeValue;
                var startsExpr = Expression.Call(memExpr, typeof(String).GetMethod("StartsWith",
                    new[] { typeof(string) }), valExpr.Body);
                return Expression.Lambda<Func<T, bool>>(startsExpr, paramExpr);
            }
        }
    
        public static Expression<Func<T, bool>> AndLike<T>(this Expression<Func<T, bool>> predicate, Expression<Func<T, string>> expr, string likeValue)
        {
            var andPredicate = Like(expr, likeValue);
            if (andPredicate != null)
            {
                predicate = predicate.And(andPredicate.Expand());
            }
            return predicate;
        }
    
        public static Expression<Func<T, bool>> OrLike<T>(this Expression<Func<T, bool>> predicate, Expression<Func<T, string>> expr, string likeValue)
        {
            var orPredicate = Like(expr, likeValue);
            if (orPredicate != null)
            {
                predicate = predicate.Or(orPredicate.Expand());
            }
            return predicate;
        }
    }
    

    var orPredicate = PredicateBuilder.False<People>();
    orPredicate = orPredicate.OrLike(per => per.Name, "He%llo%");
    orPredicate = orPredicate.OrLike(per => per.Name, "%Hi%");
    
    var predicate = PredicateBuilder.True<People>();
    predicate = predicate.And(orPredicate.Expand());
    predicate = predicate.AndLike(per => per.Status, "%Active");
    
    var list = dbContext.Set<People>().Where(predicate.Expand()).ToList();    
    

    ....
    from People per
    where (
        patindex(@p__linq__0, per.Name) <> 0
        or per.Name like @p__linq__1 escape '~'
    ) and per.Status like @p__linq__2 escape '~'
    

    ',@p__linq__0=“%He%llo%”,@p_-_linq_1=“%Hi%”,@p_linq_2=“%Active”

        9
  •  0
  •   brechtvhb    10 年前

        <Function Name="String_Like" ReturnType="Edm.Boolean">
          <Parameter Name="searchingIn" Type="Edm.String" />
          <Parameter Name="lookingFor" Type="Edm.String" />
          <DefiningExpression>
            searchingIn LIKE lookingFor
          </DefiningExpression>
        </Function>
    

    edmx:edmx/edmx:运行时/edmx:概念模型/模式

    <schema namespace="" /> 属性

    public static class Extensions
    {
        [EdmFunction("DocTrails3.Net.Database.Models", "String_Like")]
        public static Boolean Like(this String searchingIn, String lookingFor)
        {
            throw new Exception("Not implemented");
        }
    }
    

    http://jendaperl.blogspot.be/2011/02/like-in-linq-to-entities.html