惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
博客园_首页
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
罗磊的独立博客
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
Hacker News - Newest:
Hacker News - Newest: "LLM"
美团技术团队
L
LINUX DO - 最新话题
The Last Watchdog
The Last Watchdog
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
I
InfoQ
Last Week in AI
Last Week in AI
V2EX - 技术
V2EX - 技术
量子位
S
Secure Thoughts
L
LangChain Blog
The Hacker News
The Hacker News
H
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
K
Kaspersky official blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
I
Intezer
Vercel News
Vercel News
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
Google DeepMind News
Google DeepMind News
S
Securelist
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
Help Net Security
Help Net Security
Martin Fowler
Martin Fowler
爱范儿
爱范儿
Y
Y Combinator Blog
C
Check Point Blog

博客园 - 永不言败

JavaScript 语言基础知识点总结(思维导图) 关于Visual Studio项目系统属性中的宏 MongoDb的相关资料 javascript apply 和call的区别,javascript设计模式 Adding a QR Code Reader in Flex on Android castle 在mvc3如何注入 Castle Automatic Transaction Management python webpy模板介绍 使用 asp.net mvc和 jQuery UI 控件包 webpy demo核心代码 实体框架继承关系。很好 淘宝店铺图片数据迁移核心代码 mvc扩展 ASP.NET MVC 2 Templates C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现 Nhibernate继承 Castle 容器注入学习例子 javaScript 运算符 && 和 || 的返回值 event.x,event.clientX,event.offsetX区别
Howto create a System.Linq.Expressions.Expression for Like
永不言败 · 2011-04-20 · via 博客园 - 永不言败

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

Something like:

static IEnumerable<T> WhereLike<T>( 
       
this IEnumerable<T> data, 
       
string propertyOrFieldName, 
       
string value) 
{ 
   
var param = Expression.Parameter(typeof(T), "x"); 
   
var body = Expression.Call( 
       
typeof(Program).GetMethod("Like", 
           
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public), 
           
Expression.PropertyOrField(param, propertyOrFieldName), 
           
Expression.Constant(value, typeof(string))); 
   
var lambda = Expression.Lambda<Func<T, bool>>(body, param); 
   
return data.Where(lambda.Compile()); 
} 
static bool Like(string a, string b) { 
   
return a.Contains(b); // just for illustration 
} 

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

static Expression Like(Expression lhs, Expression rhs) 
{ 
   
return Expression.Call( 
       
typeof(Program).GetMethod("Like", 
           
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public) 
           
,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 });
        }
    }