zoukankan      html  css  js  c++  java
  • C# let 子句

    在查询表达式中,存储子表达式的结果有时很有帮助,可在后续子句中使用。 可以通过 let 关键字执行此操作,该关键字创建一个新的范围变量并通过提供的表达式结果初始化该变量。 使用值进行初始化后,范围变量不能用于存储另一个值。 但是,如果范围变量持有可查询类型,则可以查询该变量。

    示例

    以两种方式使用以下示例 let

    1. 创建一个可以查询其自身的可枚举类型。

    2. 使查询仅调用一次范围变量 word 上的 ToLower。 如果不使用 let,则不得不调用 where 子句中的每个谓词的 ToLower

    class LetSample1
    {
        static void Main()
        {
            string[] strings = 
            {
                "A penny saved is a penny earned.",
                "The early bird catches the worm.",
                "The pen is mightier than the sword." 
            };
    
            // Split the sentence into an array of words
            // and select those whose first letter is a vowel.
            var earlyBirdQuery =
                from sentence in strings
                let words = sentence.Split(' ')
                from word in words
                let w = word.ToLower()
                where w[0] == 'a' || w[0] == 'e'
                    || w[0] == 'i' || w[0] == 'o'
                    || w[0] == 'u'
                select word;
    
            // Execute the query.
            foreach (var v in earlyBirdQuery)
            {
                Console.WriteLine(""{0}" starts with a vowel", v);
            }
    
            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
    /* Output:
        "A" starts with a vowel
        "is" starts with a vowel
        "a" starts with a vowel
        "earned." starts with a vowel
        "early" starts with a vowel
        "is" starts with a vowel
    */
  • 相关阅读:
    关于android 中WebView使用Css
    android下面res目录
    Android View如何获取焦点
    用javascript修改html元素的class
    设计模式-观察者模式(List列表维护观察者)
    闭包->类的实例数组排序
    Javascript setTimeout 带参数延迟执行 闭包实现
    最简单的闭包 掰开揉碎
    原创最简单的ORM例子
    List<T> 转换 DataTable
  • 原文地址:https://www.cnblogs.com/dehuachenyunfei/p/7771546.html
Copyright © 2011-2022 走看看