lambda表达式Expression<Func<Person, bool>> 、Func<Person, bool>区别
前言:
自己通过lambda表达式的封装,将对应的表达式转成字符串的过程中,对lambda表达式有了新的认识
原因:
很多开发者对lambda表达式Expression<Func<Person, bool>> 、Func<Person, bool>表示存在疑惑,现在就用代码举个简单列子



原代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Lambda
{
class Program
{
static void Main(string[] args)
{
Expression<Func<Person, bool>> exp = null;
Func<Person, string> func = null;
func = p => { return p.age; };
exp = p => p.sex=="男";
Person person = new Person();
person.age = "12";
person.name = "zouhp";
person.sex = "男";
Console.WriteLine(exp);
Console.WriteLine(exp.Compile()(person));
Console.WriteLine(func(person));
Console.ReadLine();
}
public class Person
{
public string name;
public string age;
public string sex;
}
}
}
结论:
Func<TObject, bool>是委托(delegate)
Expression<Func<TObject, bool>>是表达式
Expression编译后就会变成delegate,才能运行。比如
Expression<Func<int, bool>> ex = x=>x < 100;
Func<int, bool> func = ex.Compile();
然后你就可以调用func:
func(5) //-返回 true
func(200) //- 返回 false
而表达式是不能直接调用的。
参考:http://stackoverflow.com/questions/793571/why-would-you-use-expressionfunct-rather-than-funct
关于EF中用哪个你可以看看这篇文章:Entity Framework - Func引起的数据库全表查询
关于如何将多个expression合并为一个可以写多个where:
.where(expression1).where(expression2)...
运行时EF会自动合并优化的
lambda表达式Expression<Func<Person, bool>> 、Func<Person, bool>区别的更多相关文章
- Entity Framework 动态构造Lambda表达式Expression<Func<T, bool>>
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...
- 对委托 以及 action func 匿名函数 以及 lambda表达式的简单记录
class Program { public delegate void MyDelegate(string str); static void Main(string[] args) { // My ...
- 动态组合lambda 表达式
//记录实体集合—动态组合lambda 表达式 Expression<Func<AdEntity, bool>> thirdWhere = p => p.Observer ...
- C# 3.0 / C# 3.5 Lambda 表达式
概述 Lambda 表达式的本质就是匿名函数.(而匿名方法的本质是委托) “Lambda 表达式”是一个匿名函数,可以包含表达式和语句,并且可用于创建委托或表达式树类型. (Lambda 表达式的运算 ...
- APS.NET MVC + EF (01)---Linq和Lambda表达式
1.1 Linq介绍 LINQ全称 Language Integrated Query(语言集成查询).为我们提供一种统一的方式来查询和操作各种数据. LINQ to Object:是针对实现了IEn ...
- C#在泛型类中,通过表达式树构造lambda表达式
场景 最近对爬虫的数据库架构做调整,需要将数据迁移到MongoDB上去,需要重新实现一个针对MongoDB的Dao泛型类,好吧,动手开工,当实现删除操作的时候问题来了. 我们的删除操作定义如下:voi ...
- Python函数式编程之lambda表达式
一:匿名函数的定义 lambda parameter_list: expression 二:三元表达式 条件为真时返回的结果 if 条件判断 else 条件为假的时候返回的结果 三:map map(f ...
- C#基础知识---Lambda表达式
一.Lambda表达式简介 Lambda表达式可以理解为匿名函数,可以包含表达式和语句.它提供了一种便利的形式来创建委托. Lambda表达式使用这个运算符--- "=>", ...
- python中lambda表达式应用
对于简单的函数,也存在一种简便的表示方式,即:lambda表达式 #普通函数1 def func(a): return a+1 print 'test1_func0:',func(1000)4#lam ...
随机推荐
- Yii2 Ajax Post 实例及常见错误修正
先贴下我的代码: signup.js$('.reg_verify_pic').click(function(){ var csrfToken = $('meta[name="_csrf-To ...
- Hibernate Error: a different object with the same identifier value was already associated with the session
在执行Hibernate的Update操作时,报错:a different object with the same identifier value was already associated w ...
- 【BZOJ2127】happiness
Time Limit: 1000 ms Memory Limit: 256 MB Description 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为 ...
- 用户态Linux内核
User Mode Linux 是可以在用户态启动的 Linux版本,最新版linux内核已提供了支持.这使我们能在类似 OpenVZ 虚拟化技术的系统上,使用最新的 Linux 内核:并且可以在非 ...
- input placeholder样式
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color:red; } input:-moz-plac ...
- Linux OpenSSH后门的添加与防范
引言:相对于Windows,Linux操作系统的密码较难获取.不过很多Linux服务器配置了OpenSSH服务,在获取root权限的情况下,可以通过修改或者更新OpenSSH代码等方法,截取并保存其S ...
- Sparklyr与Docker的推荐系统实战
作者:Harry Zhu 链接:https://zhuanlan.zhihu.com/p/21574497 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 相关内容: ...
- iframe父页面获取子页面的参数
1.父页面中的iframe <iframe name="parentPage"></iframe> 2.子页面中元素的属性 <input type=& ...
- Indent Guides for Visual Studio 代码格式化收缩插件
代码格式化收缩插件 下载地址:http://indentguide.codeplex.com/releases/view/110312 更多关注付义方技术博客:http://blog.csdn.net ...
- freemarker.core.ParseException:Unexpected end of file reached
1.错误原因 freemarker.core.ParseException:Unexpected end of file reached 2.错误原因 由于在宏定义中,运用组件时没有关闭标签,导致出错 ...