以前用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引起的数据库全表查询

通过已有Func构造Expression表达式问题

Expression<Func<T>>和Func<T>的更多相关文章

  1. Expression<Func<T,TResult>>和Func<T,TResult> 与AOP与WCF

    1>>Expression<Func<T,TResult>>和Func<T,TResult>http://www.cnblogs.com/xcsn/p/ ...

  2. Expression<Func<TObject, bool>>与Func<TObject, bool>的区别

    Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后 ...

  3. lambda表达式Expression<Func<Person, bool>> 、Func<Person, bool>区别

    前言: 自己通过lambda表达式的封装,将对应的表达式转成字符串的过程中,对lambda表达式有了新的认识 原因: 很多开发者对lambda表达式Expression<Func<Pers ...

  4. Expression<Func<T, bool>>与Func<T, bool>的区别

    转自:http://www.cnblogs.com/wow-xc/articles/4952233.html Func<TObject, bool>是委托(delegate) Expres ...

  5. EF Core 封装方法Expression<Func<TObject, bool>>与Func<TObject, bool>区别

    unc<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后就 ...

  6. expression<Func<object,Bool>> 及 Func<oject,bool>用法

    using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using ...

  7. Expression<Func<T,TResult>>和Func<T,TResult>

    1.Expression<Func<T,TResult>>是表达式 //使用LambdaExpression构建表达式树 Expression<Func<int, ...

  8. 从var func=function 和 function func()区别谈Javascript的预解析机制

    var func=function 和 function func()在意义上没有任何不同,但其解释优先级不同:后者会先于同一语句级的其他语句. 即: { var k = xx(); function ...

  9. onclick="func()"和 onclick = "return func()"区别

    onclick="func()" 表示只会执行 func , 但是不会传回 func 中之回传值onclick = "return func()" 则是 执行 ...

  10. 【转】int const A::func()和int A::func() const

    int const A::func() { return 0; }int A::func() const { return 0; } 上面的代码是合法的,其中A::func成员函数[只能在成员函数后面 ...

随机推荐

  1. C#通过字符串名称来调用对应字符串名称的方法

    前段时间在一个项目中,在一个地方要将函数所在类中的方法都调用一遍,但是否调用要通过配置文件中的内容决定.所以为了减少代码量,在网上查了相关信息,终于成功的将其应用到了项目中,我在这里将我做的一个简单例 ...

  2. ASP.NET MVC 实现有论坛功能的网站(有iis发布网站)

    ASP.NET MVC. M 为Model模型层, V 为View视图层, C 为Controller控制层.要想使用MVC框架来写网站就需要了解M V C 的作用分别为哪些.给大家简单的介绍一下: ...

  3. AEAI DP开发统计分析

    1 背景概述 平时做统计分析都是调rest服务,给前台提供数据,然后在管理控制台里配置portlet.但并不是所有的项目都会用到portal,这时就需要在AEAI DP应用开发平台里开发统计分析了,下 ...

  4. CentOS 7 - 创建新用户

    当进行服务器操作时,我们尽量不要使用root用户进行操作,特别是当我们使用生产环境时. 本文我们将介绍CentOS 7下用户的创建. 创建新用户 adduser 用户名 更改用户密码 passwd 用 ...

  5. 【BZOJ1049】 [HAOI2006]数字序列

    BZOJ1049 [HAOI2006]数字序列 dp好题? 第一问 第一问我会做!令\(b_i=a_i-i\),求一个最长不下降子序列. \(n-ans\)就是最终的答案. 第二问 好难啊.不会.挖坑 ...

  6. 多用户在线人数监听(基于TomCat)

    服务器Servlet端 package com.sxt.mvcpro.servlet; import java.io.IOException; import java.util.HashSet; im ...

  7. 【sping揭秘】2、关于spring配置文件

    <import>标签 引入其他的配置文件,如果A.xml 中的<bean>定义可能依赖B.xml中的某些<bean>定义,那么可以再A.xml中使用这种方式< ...

  8. idea 安装mybatis plugin (mybatis插件)

    注意:可以用免费版本的,就是下面没有 被红框圈中的 Free Mybatis Plugin 安装上以后需要破解,先找到下面的文件 打开文件,设置其中的key 和 value : 这里面的key 和 v ...

  9. Spring WebSocket踩坑指南

    Spring WebSocket踩坑指南 本次公司项目中需要在后台与安卓App间建立一个长连接,这里采用了Spring的WebSocket,协议为Stomp. 关于Stomp协议这里就不多介绍了,网上 ...

  10. [Umbraco] macro(宏)在umbraco中的作用

    macro在umbraco中是一个核心的应用,它是模板页中用于动态加载内容的标签(模板指令),宏可以是基于XSLT文件创建,亦可以是基于ASP.NET用户控件创建 在develop下的Macros中创 ...