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

推荐订阅源

雷峰网
雷峰网
MyScale Blog
MyScale Blog
PCI Perspectives
PCI Perspectives
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
Lohrmann on Cybersecurity
C
Cybersecurity and Infrastructure Security Agency CISA
Spread Privacy
Spread Privacy
量子位
P
Proofpoint News Feed
T
Tailwind CSS Blog
腾讯CDC
G
Google Developers Blog
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
GbyAI
GbyAI
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
A
About on SuperTechFans
有赞技术团队
有赞技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
Recorded Future
Recorded Future
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
aimingoo的专栏
aimingoo的专栏
美团技术团队
Engineering at Meta
Engineering at Meta
博客园 - 【当耐特】
小众软件
小众软件
F
Fortinet All Blogs
Last Week in AI
Last Week in AI
U
Unit 42
T
The Blog of Author Tim Ferriss
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
S
SegmentFault 最新的问题
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
A
Arctic Wolf
C
CERT Recently Published Vulnerability Notes
Security Latest
Security Latest
L
LINUX DO - 热门话题

博客园 - Ant

ASP.NET Core 项目简单实现身份验证及鉴权 使用iconfont图标 IIS调试ASP.NET Core项目 配置GitHub的SSH key Visual Studio连接Oracle数据库 Visual Studio Code 如何将新项目发布到GIT服务器 Xamarin踩坑经历 Abp项目中的领域模型实体类访问仓储的方法 在Abp中集成Swagger UI功能 发布以NLog作为日记工具的ASP.NET站点到IIS注意事项 Android Studio 简介及导入 jar 包和第三方开源库方[转] 解决jquery-ui-autocomplete选择列表被Bootstrap模态窗遮挡的问题 ORACLE存储过程创建失败,如何查看其原因 EF6配合MySQL或MSSQL(CodeFirst模式)配置指引 火狐通行证升级为Firefox Sync后,如何在多设备间同步书签等信息 (原创)通用查询实现方案(可用于DDD)[附源码] -- 设计思路 (原创)通用查询实现方案(可用于DDD)[附源码] -- 简介 利用Lambda获取属性名称 [转]Sql server2005中如何格式化时间日期
Entity Framework 6.0 源码解读笔记(一)
Ant · 2014-05-15 · via 博客园 - Ant
        internal static TResult ExecuteSingle<TResult>(IEnumerable<TResult> query, Expression queryRoot)
        {
            return GetElementFunction<TResult>(queryRoot)(query); //①
        }

        private static Func<IEnumerable<TResult>, TResult> GetElementFunction<TResult>(Expression queryRoot)
        {
            SequenceMethod seqMethod;
            if (ReflectionUtil.TryIdentifySequenceMethod(queryRoot, true /*unwrapLambdas*/, out seqMethod))
            {
                switch (seqMethod)
                {
                    case SequenceMethod.First:
                    case SequenceMethod.FirstPredicate:
                        return (sequence) => { return sequence.First(); }; //②

                    case SequenceMethod.FirstOrDefault:
                    case SequenceMethod.FirstOrDefaultPredicate:
                        return (sequence) => { return sequence.FirstOrDefault(); };

                    case SequenceMethod.SingleOrDefault:
                    case SequenceMethod.SingleOrDefaultPredicate:
                        return (sequence) => { return sequence.SingleOrDefault(); };
                }
            }

            return (sequence) => { return sequence.Single(); };
        }

很久没学习C#新的语法了,读到这段代码时基本上懵了,只知道是Lambda表达式,但是比我平时所使用Lambda表达式可复杂多了。

首先,我们来看GetElementFunction()方法,它返回的是一个方法委托,该委托的具有一个参数(即IEnumerable<TResult>)并返回一个指定类型的值(即TResult类)

第①处代码分两步执行,第1步执行GetElementFunction<TResult>(queryRoot),我们假定执行的分支为第②处代码,那么实际返回的Lambda表达:(sequence) => { return sequence.First(); };

该表达式可解读为下面的方法

TResult AnonymousMethod(IEnumerable<TResult> sequence)
{
  return sequence.First();
}

第2步,调用Lambda表达式的匿名方法,并将query参数传给它,Lambda表达式中sequence变量即等价于传入的query变量,相当于调用AnonymousMethod(query)

总结:重点代码是上面标注了①②的两行,执行这条分支的代码相当于调用query.First();