步步为营VS 2008 + .NET 3.5(4) - C# 3.0新特性之LambdaExpressions(Lambda表达式)-阿里云开发者社区

开发者社区> 技术小胖子> 正文

步步为营VS 2008 + .NET 3.5(4) - C# 3.0新特性之LambdaExpressions(Lambda表达式)

简介:
+关注继续查看
[索引页]
[源码下载]


步步为营VS 2008 + .NET 3.5(4) - C# 3.0新特性之LambdaExpressions(Lambda表达式)、QuerySyntax(查询语法)和AnonymousTypes(匿名类型)


作者:webabcd


介绍
    ·LambdaExpressions(Lambda表达式)
    ·QuerySyntax(查询语法)
    ·AnonymousTypes(匿名类型)


示例
LambdaExpressions.cs(Lambda表达式)
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gifusing System.Xml.Linq; 
InBlock.gif 
InBlock.gifusing System.Collections.Generic; 
InBlock.gif 
/// <summary> 
/// LambdaExpressions(Lambda表达式)的摘要说明 
/// </summary> 
InBlock.gifpublic class LambdaExpressions 
InBlock.gif
InBlock.gif        public int ID { get; set; } 
InBlock.gif        public string Name { get; set; } 
InBlock.gif 
InBlock.gif        public void LambdaExpressionsTest() 
InBlock.gif        { 
InBlock.gif                List<LambdaExpressions> list = new List<LambdaExpressions> 
InBlock.gif                { 
InBlock.gif                        new LambdaExpressions { ID = 1, Name = "webabcd" }, 
InBlock.gif                        new LambdaExpressions { ID = 2, Name = "webabcdefg" }, 
InBlock.gif                        new LambdaExpressions { ID = 3, Name = "webabcdefghijklmn" } 
InBlock.gif                }; 
InBlock.gif 
InBlock.gif                IEnumerable<LambdaExpressions> l = list.Where(le => le.Name == "webabcd"); 
InBlock.gif 
InBlock.gif                // 上面的(Lambda表达式)等同于下面的(匿名方法) 
InBlock.gif 
InBlock.gif                // IEnumerable<LambdaExpressions> l2 = list.Where(delegate(LambdaExpressions le) { return le.Name == "webabcd"; }); 
InBlock.gif 
InBlock.gif 
InBlock.gif 
InBlock.gif                // 相关委托 
InBlock.gif                // public delegate TResult Func<T, TResult>(T arg); 
InBlock.gif            
InBlock.gif                // 相关Where扩展方法 
InBlock.gif                // Func<TSource, bool>:接受一个类型为TSource的参数 
InBlock.gif                // Func<TSource, bool>:某个需要满足的条件,返回bool值 
InBlock.gif                // public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) 
InBlock.gif                // { 
InBlock.gif                //         foreach (TSource item in source) 
InBlock.gif                //         { 
InBlock.gif                //                 if (predicate(item)) 
InBlock.gif                //                 { 
InBlock.gif                //                         yield return item; 
InBlock.gif                //                 } 
InBlock.gif                //         } 
InBlock.gif                // } 
InBlock.gif                 
InBlock.gif        } 
InBlock.gif}
 

QuerySyntax.cs(查询语法)
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gifusing System.Xml.Linq; 
InBlock.gif 
InBlock.gifusing System.Collections.Generic; 
InBlock.gif 
/// <summary> 
/// QuerySyntax(查询语法)的摘要说明 
/// </summary> 
InBlock.gifpublic class QuerySyntax 
InBlock.gif
InBlock.gif        public int ID { get; set; } 
InBlock.gif        public string Name { get; set; } 
InBlock.gif 
InBlock.gif        public void QuerySyntaxTest() 
InBlock.gif        { 
InBlock.gif                List<QuerySyntax> list = new List<QuerySyntax> 
InBlock.gif                { 
InBlock.gif                        new QuerySyntax { ID = 1, Name = "webabcd" }, 
InBlock.gif                        new QuerySyntax { ID = 2, Name = "webabcde" }, 
InBlock.gif                        new QuerySyntax { ID = 2, Name = "webabcdef" }, 
InBlock.gif                        new QuerySyntax { ID = 2, Name = "webabcdefg" }, 
InBlock.gif                        new QuerySyntax { ID = 2, Name = "webabcdefgh" }, 
InBlock.gif                        new QuerySyntax { ID = 2, Name = "webabcdefghi" }, 
InBlock.gif                        new QuerySyntax { ID = 2, Name = "webabcdefghij" }, 
InBlock.gif                        new QuerySyntax { ID = 2, Name = "webabcdefghijk" }, 
InBlock.gif                        new QuerySyntax { ID = 2, Name = "webabcdefghijkl" }, 
InBlock.gif                        new QuerySyntax { ID = 2, Name = "webabcdefghijklm" }, 
InBlock.gif                        new QuerySyntax { ID = 3, Name = "webabcdefghijklmn" } 
InBlock.gif                }; 
InBlock.gif 
InBlock.gif                IEnumerable<QuerySyntax> l = from o in list 
InBlock.gif                                                                         where o.Name.Length > 10 
InBlock.gif                                                                         orderby o.Name.Length descending 
InBlock.gif                                                                         select o; 
InBlock.gif 
InBlock.gif                // 上面的(查询语法)等同于下面的(LINQ扩展方法和Lambda表达式) 
InBlock.gif                // 查询语法相对更容易理解 
InBlock.gif 
InBlock.gif                // IEnumerable<QuerySyntax> l = list.Where(o => o.Name.Length > 10).OrderByDescending(o => o.Name.Length); 
InBlock.gif 
InBlock.gif 
InBlock.gif 
InBlock.gif                // Projection(映射) 
InBlock.gif                // 可以返回一个新的类型 
InBlock.gif                IEnumerable<Projection> l2 = from o in list 
InBlock.gif                                                                            where o.Name.Length > 10 
InBlock.gif                                                                            orderby o.Name.Length descending 
InBlock.gif                                                                            select new Projection { Name = o.Name }; 
InBlock.gif        } 
InBlock.gif
InBlock.gif 
/// <summary> 
/// 为了演示Projection(映射)而写的实体类 
/// </summary> 
InBlock.gifpublic class Projection 
InBlock.gif
InBlock.gif        public string Name { get; set; } 
InBlock.gif}
 
AnonymousTypes.cs匿名类型
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gifusing System.Xml.Linq; 
InBlock.gif 
InBlock.gifusing System.Collections.Generic; 
InBlock.gif 
/// <summary> 
/// AnonymousTypes(匿名类型)的摘要说明 
/// </summary> 
InBlock.gifpublic class AnonymousTypes 
InBlock.gif
InBlock.gif        public int ID { get; set; } 
InBlock.gif        public string Name { get; set; } 
InBlock.gif        public int Age { get; set; } 
InBlock.gif 
InBlock.gif        public void AnonymousTypesTest() 
InBlock.gif        { 
InBlock.gif                List<AnonymousTypes> list = new List<AnonymousTypes> 
InBlock.gif                { 
InBlock.gif                        new AnonymousTypes { ID = 1, Name = "webabcd", Age = 10 }, 
InBlock.gif                        new AnonymousTypes { ID = 2, Name = "webabcdefg", Age = 20 }, 
InBlock.gif                        new AnonymousTypes { ID = 3, Name = "webabcdefghijklmn", Age = 30 } 
InBlock.gif                }; 
InBlock.gif 
InBlock.gif                // listAnonymousTypes - 匿名类型 
InBlock.gif                var listAnonymousTypes = from l in list 
InBlock.gif                                                                 where l.Name == "webabcd" 
InBlock.gif                                                                 select new { Name = l.Name, Age = l.Age }; 
InBlock.gif 
InBlock.gif                foreach (var v in listAnonymousTypes) 
InBlock.gif                { 
InBlock.gif                        // v - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense 
InBlock.gif                        string name = v.Name; 
InBlock.gif                        int age = v.Age; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                // 声明匿名类型:将new关键词后面的类型名称省略掉 
InBlock.gif                var person = new { Name = "webabcd", Age = 27 }; 
InBlock.gif                // person - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense 
InBlock.gif                string myName = person.Name; 
InBlock.gif                int myAge = person.Age; 
InBlock.gif        } 
InBlock.gif}
 
 
 



     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344994,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
C#委托基础8——lambda表达式
  C#委托基础系列原于2011年2月份发表在我的新浪博客中,现在将其般至本博客。 class Program { double AddInt(int x, int y) { return x + y; } string AddString(string s1, string s2)
801 0
Java8 Lambda表达式详解手册及实例
Java8 Lambda表达式详解手册及实例
7 0
降低 80% 的读写响应延迟!我们测评了 etcd 3.4 新特性(内含读写发展史)
作者 | 陈洁(墨封)  阿里云开发工程师 导读:etcd 作为 K8s 集群中的存储组件,读写性能方面会受到很多压力,而 etcd 3.4 中的新特性将有效缓解压力,本文将从 etcd 数据读写机制的发展历史着手,深入解读 etcd 3.4 新特性。
1397 0
Java语法糖--Lambda表达式存在的意义
本文目录 1. Lambda表达式的本质 2. 语法 2.1 通过类实现加密 2.2 通过匿名内部类实现加密 2.3 通过Lambda表达式实现加密 2.4 Lambda表达式的简略写法 3. Lambda表达式存在的意义 4. 函数式接口
6 0
Python学习笔记:lambda表达式与函数式编程
1,lambda的一般形式是关键字lambda后面跟一个或多个参数,紧跟一个冒号,以后是一个表达式。lambda是一个表达式而不是一个语句。它能够出现在Python语法不允许def出现的地方。作为表达式,lambda返回一个值(即一个新的函数)。
1304 0
21119
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载