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


    [索引页]
    [源码下载]


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


    作者:webabcd


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


    示例
    LambdaExpressions.cs(Lambda表达式)
    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml.Linq;

    using System.Collections.Generic;

    /// <summary>
    /// LambdaExpressions(Lambda表达式)的摘要说明
    /// </summary>

    public class LambdaExpressions
    {
        
    public int ID getset; }
        
    public string Name getset; }

        
    public void LambdaExpressionsTest()
        
    {
            List
    <LambdaExpressions> list = new List<LambdaExpressions>
            
    {
                
    new LambdaExpressions { ID = 1, Name = "webabcd" },
                
    new LambdaExpressions { ID = 2, Name = "webabcdefg" },
                
    new LambdaExpressions { ID = 3, Name = "webabcdefghijklmn" }
            }
    ;

            IEnumerable
    <LambdaExpressions> l = list.Where(le => le.Name == "webabcd");

            
    // 上面的(Lambda表达式)等同于下面的(匿名方法)

            
    // IEnumerable<LambdaExpressions> l2 = list.Where(delegate(LambdaExpressions le) { return le.Name == "webabcd"; });



            
    // 相关委托
            
    // public delegate TResult Func<T, TResult>(T arg);
         
            
    // 相关Where扩展方法
            
    // Func<TSource, bool>:接受一个类型为TSource的参数
            
    // Func<TSource, bool>:某个需要满足的条件,返回bool值
            
    // public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
            
    // {
            
    //     foreach (TSource item in source)
            
    //     {
            
    //         if (predicate(item))
            
    //         {
            
    //             yield return item;
            
    //         }
            
    //     }
            
    // }
            
        }

    }


    QuerySyntax.cs(查询语法)
    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml.Linq;

    using System.Collections.Generic;

    /// <summary>
    /// QuerySyntax(查询语法)的摘要说明
    /// </summary>

    public class QuerySyntax
    {
        
    public int ID getset; }
        
    public string Name getset; }

        
    public void QuerySyntaxTest()
        
    {
            List
    <QuerySyntax> list = new List<QuerySyntax>
            
    {
                
    new QuerySyntax { ID = 1, Name = "webabcd" },
                
    new QuerySyntax { ID = 2, Name = "webabcde" },
                
    new QuerySyntax { ID = 2, Name = "webabcdef" },
                
    new QuerySyntax { ID = 2, Name = "webabcdefg" },
                
    new QuerySyntax { ID = 2, Name = "webabcdefgh" },
                
    new QuerySyntax { ID = 2, Name = "webabcdefghi" },
                
    new QuerySyntax { ID = 2, Name = "webabcdefghij" },
                
    new QuerySyntax { ID = 2, Name = "webabcdefghijk" },
                
    new QuerySyntax { ID = 2, Name = "webabcdefghijkl" },
                
    new QuerySyntax { ID = 2, Name = "webabcdefghijklm" },
                
    new QuerySyntax { ID = 3, Name = "webabcdefghijklmn" }
            }
    ;

            IEnumerable
    <QuerySyntax> l = from o in list
                                         where o.Name.Length 
    > 10
                                         orderby o.Name.Length descending
                                         select o;

            
    // 上面的(查询语法)等同于下面的(LINQ扩展方法和Lambda表达式)
            
    // 查询语法相对更容易理解

            
    // IEnumerable<QuerySyntax> l = list.Where(o => o.Name.Length > 10).OrderByDescending(o => o.Name.Length);



            
    // Projection(映射)
            
    // 可以返回一个新的类型
            IEnumerable<Projection> l2 = from o in list
                                          where o.Name.Length 
    > 10
                                          orderby o.Name.Length descending
                                          select 
    new Projection { Name = o.Name };
        }

    }


    /// <summary>
    /// 为了演示Projection(映射)而写的实体类
    /// </summary>

    public class Projection
    {
        
    public string Name getset; }
    }


    AnonymousTypes.cs匿名类型
    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml.Linq;

    using System.Collections.Generic;

    /// <summary>
    /// AnonymousTypes(匿名类型)的摘要说明
    /// </summary>

    public class AnonymousTypes
    {
        
    public int ID getset; }
        
    public string Name getset; }
        
    public int Age getset; }

        
    public void AnonymousTypesTest()
        
    {
            List
    <AnonymousTypes> list = new List<AnonymousTypes>
            
    {
                
    new AnonymousTypes { ID = 1, Name = "webabcd", Age = 10 },
                
    new AnonymousTypes { ID = 2, Name = "webabcdefg", Age = 20 },
                
    new AnonymousTypes { ID = 3, Name = "webabcdefghijklmn", Age = 30 }
            }
    ;

            
    // listAnonymousTypes - 匿名类型
            var listAnonymousTypes = from l in list
                                     where l.Name 
    == "webabcd"
                                     select 
    new { Name = l.Name, Age = l.Age };

            
    foreach (var v in listAnonymousTypes)
            
    {
                
    // v - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense
                string name = v.Name;
                
    int age = v.Age;
            }


            
    // 声明匿名类型:将new关键词后面的类型名称省略掉
            var person = new { Name = "webabcd", Age = 27 };
            
    // person - 匿名类型,可以在Visual Studio中得到编译时检查和完整的intellisense
            string myName = person.Name;
            
    int myAge = person.Age;
        }

    }


    OK
    [源码下载]
  • 相关阅读:
    【LeetCode】【动态规划】Edit Distance
    【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)
    【LeetCode】【矩阵旋转】Rotate Image
    解决Torch.load()错误信息: UnicodeDecodeError: 'ascii' codec can't decode byte 0x8d in position 0: ordinal not in range(128)
    使用VS Code配合Remote Development插件连接远程服务器(Mac/Linux+Windows) | Using VS Code with Remote Development Connect to Remote Server (Mac/Linux+Windows)
    Leaflet入门:添加点线面并导入GeoJSON数据|Tutorial of Leaflet: Adding Points, Lines, Polygons and Import GeoJSON File
    使用Adobe Illustrator + ArcGIS绘制地图 | Map Design Using ArcGIS + Adobe Illustrator
    PostgreSQL 速查、备忘手册 | PostgreSQL Quick Find and Tutorial
    LIRE教程之源码分析 | LIRE Tutorial of Analysis of the Source Code
    解决Tomcat错误信息:No 'Access-Control-Allow-Origin' header is present on the requested resource | Solving Tomcat Error: No 'Access-Control-Allow-Origin' header is present on the requested resource
  • 原文地址:https://www.cnblogs.com/dajiang02/p/1359907.html
Copyright © 2020-2023  润新知