Expression<Func<T>>和Func<T>
以前用EF的时候,由于where的时候有Expression<Func<T>>和Func<T>两种查询条件,误用了Func<T>那个重载,后来还想通过func创建查询来着,不过失败了,导致了全表查询,真是无语.国内的人答的比较言简意赅(其实我觉得讲的不好).还是老外讲的明白点.
翻译过来吧,就是说Func<T>是方法的委托,而Expression<Func<T>>是拉姆达表达式树.这个树状结构描述了各种各样恶心的参数(如下图所示).我们可以用Expression.Compile做成一个委托或者编译成sql(EF).
Expression<Func<int>> myExpression = () => 10;

其实吧, 多用一下你就知道了.Func<T>用的还蛮多的,当时就是用来运行泛化的方法的,而Expression<Func<T>>用在动态查询拼接的时候比较多,比如 (And和or,拼接多条表达式树).
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose<T>(second, new Func<Expression, Expression, Expression>(Expression.And));
} private static Expression<Func<T, bool>> Compose<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second, Func<Expression, Expression, Expression> merge)
{
Expression expression = new ParameterRebinder(second.Parameters[0], first.Parameters[0]).Visit(second.Body);
return Expression.Lambda<Func<T, bool>>(merge(first.Body, expression), first.Parameters);
} public static Expression<Func<T, bool>> False<T>()
{
return item => false;
} public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> predicate)
{
return Expression.Lambda<Func<T, bool>>(Expression.Not(predicate.Body), predicate.Parameters);
} public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose<T>(second, new Func<Expression, Expression, Expression>(Expression.Or));
} public static Expression<Func<T, bool>> True<T>()
{
return item => true;
} private sealed class ParameterRebinder : ExpressionVisitor
{
private readonly ParameterExpression m_From;
private readonly ParameterExpression m_To; public ParameterRebinder(ParameterExpression from, ParameterExpression to)
{
this.m_From = from;
this.m_To = to;
} protected override Expression VisitParameter(ParameterExpression node)
{
if (node == this.m_From)
{
node = this.m_To;
}
return base.VisitParameter(node);
}
} }
表达式树恶心的地方,我写一个orderby给你看看.
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty, bool desc)
{
string command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(TEntity);//实体的类型
var property = type.GetProperty(orderByProperty);
var parameter = Expression.Parameter(type, "o");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExpression));
return source.Provider.CreateQuery<TEntity>(resultExpression);
}
动态linq是需要反射的.而且这种写法不利于调试.因为你特么完全不知道生成的什么鬼,除非你对这玩意真的很熟.好吧,你赢了.
参考链接:
Why would you use Expression<Func<T>> rather than Func<T>?
Entity Framework - Func引起的数据库全表查询
Expression<Func<T>>和Func<T>的更多相关文章
- Expression<Func<T,TResult>>和Func<T,TResult> 与AOP与WCF
1>>Expression<Func<T,TResult>>和Func<T,TResult>http://www.cnblogs.com/xcsn/p/ ...
- Expression<Func<TObject, bool>>与Func<TObject, bool>的区别
Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后 ...
- lambda表达式Expression<Func<Person, bool>> 、Func<Person, bool>区别
前言: 自己通过lambda表达式的封装,将对应的表达式转成字符串的过程中,对lambda表达式有了新的认识 原因: 很多开发者对lambda表达式Expression<Func<Pers ...
- Expression<Func<T, bool>>与Func<T, bool>的区别
转自:http://www.cnblogs.com/wow-xc/articles/4952233.html Func<TObject, bool>是委托(delegate) Expres ...
- EF Core 封装方法Expression<Func<TObject, bool>>与Func<TObject, bool>区别
unc<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后就 ...
- expression<Func<object,Bool>> 及 Func<oject,bool>用法
using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using ...
- Expression<Func<T,TResult>>和Func<T,TResult>
1.Expression<Func<T,TResult>>是表达式 //使用LambdaExpression构建表达式树 Expression<Func<int, ...
- 从var func=function 和 function func()区别谈Javascript的预解析机制
var func=function 和 function func()在意义上没有任何不同,但其解释优先级不同:后者会先于同一语句级的其他语句. 即: { var k = xx(); function ...
- onclick="func()"和 onclick = "return func()"区别
onclick="func()" 表示只会执行 func , 但是不会传回 func 中之回传值onclick = "return func()" 则是 执行 ...
- 【转】int const A::func()和int A::func() const
int const A::func() { return 0; }int A::func() const { return 0; } 上面的代码是合法的,其中A::func成员函数[只能在成员函数后面 ...
随机推荐
- Winform DataGridView控件在业务逻辑上的简单使用
需要对文字列表进行处理,然后用到DataGridView控件来处理,记录一下.效果如下: 主要是想通过禁用和取消单元格选择来使图标单元格呈现出鼠标点击的效果.因为有个单元格选择的问题困扰着我. 是这样 ...
- 面向对象总结、configparser配置文件模块、logging日志模块
面向对象总结 # 学习态度# python基础 2个月# html css js jq 1个月 # 上课困 # 学习方法 :# 列出知识点# 例子 写了哪些 # 面向对象学了哪些块# 为什么要讲面向对 ...
- SpringBoot使用ModelAndView时配置视图解析器
spring boot 使用视图modelandview 原文:https://www.cnblogs.com/liyafei/p/7955943.html 1:springboot使用视图解析器,添 ...
- Python 生成器, 迭代器, 可迭代对象的区别
1.可迭代对象 在python中, 一切皆对象, 但凡是可以用for循环进行遍历取值的对象都可以称之为可迭代对象, 可迭代对象在程序的一个执行周期中,可以无限轮次的进行循环遍历 2.迭代器 a.一个可 ...
- 初探日志框架Logback
一. 背景 最近因为学习项目时需要使用logback日志框架来打印日志, 使用过程中碰到很多的疑惑, 而且需要在控制台打印mybatis执行的sql语句, 于是决定沉下心来 研究一下logback的使 ...
- Mac使用brew安装nginx,并解决端口访问权限问题
1.安装 brew install nginx 2.修改配置文件 sudo vi /usr/local/etc/nginx/nginx.conf 修改默认的8080端口为80 修改日志文件地方 err ...
- 解压cpio.gz
#gunzip 文件名.cpio.gz #cpio -idmv < 文件名.cpio
- 善用Intellij插件可大幅提升我们的效率
转自 :https://www.jianshu.com/p/686ba0ae4ac2 1. .ignore 生成各种ignore文件,一键创建git ignore文件的模板,免得自己去写 截图: 2. ...
- editplus来编写html
本来写这篇文章,我可以有很多废话,但是很多都过去了,言而总之下:我暂且给这个方法起个名字,叫做“为之法”,因为有了这篇文章,很多人的想法会豁然开朗,那样有了个名字交流传阅起来就方便多了. 本方法依托于 ...
- odoo第三方市场 -- 模块推荐
odoo 除了开源,另一个非常给力的地方就是,强大的第三方应用市场: 你入坑后,会发现非常的好玩,全球还有这么多小伙伴并肩前行,共同成长. 第三方市场有很多不错的模块,当然,好东西,不是完全免费的! ...