Howto create a System.Linq.Expressions.Expression for Like

http://stackoverflow.com/questions/956743/howto-create-a-system-linq-expressions-expression-for-like

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

http://www.albahari.com/nutshell/predicatebuilder.aspx

6 down vote accepted

Something like:

 <T> <T>( 
       
<T> data, 
       
propertyOrFieldName, 
       
value) 
{ 
   
param = .((T), ); 
   
body = .( 
       
().(, 
           
. | . | .), 
           
.(param, propertyOrFieldName), 
           
.(value, ())); 
   
lambda = .<<T, bool>>(body, param); 
   
data.(lambda.()); 
} 
bool ( a, b) { 
   
a.(b);  
} 

 


 

In terms of a Func<Expression,Expression,Expression>:

  ( lhs,  rhs) 
{ 
   
.( 
       
().(, 
           
. | . | .) 
           
,lhs,rhs); 
} 

 

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
 
    public static class LinqExtensions
    {
        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));
        }
 
        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);
        }
 
        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 });
        }
    }

 

转载于:https://www.cnblogs.com/genson/archive/2011/04/20/2022236.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值