步步为营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,如需转载请自行联系原作者

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

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章