Let clause of LInq

Source: Internet
Author: User
   1:  class Program
   2:      {
   3:          /// <summary>
4: // let clause of LInq
5: // let: stores the results of a subexpression, which can be used in subsequent clauses.
6: // use the let keyword to complete this task. This keyword can create a new range variable and initialize the variable with the result of the expression you provide.
7: // once the range variable is initialized with a value, it cannot be used to store other values
8: // If the variable in this range is of the queryable type, you can query it.
   9:          /// </summary>
  10:          /// <param name="args"></param>
  11:          static void Main(string[] args)
  12:          {
13:/* instance:
14: * in the following example, let is used in two ways:
15: 1. Create an enumerative type that can be queried.
16: 2. Make the query only call ToLower once for the range variable word. If you do not use let, you must call ToLower in each predicate of the where clause.
  17:              */
  18:              string[] text = 
  19:              {
  20:                "A penny saved is a penny earned.",
  21:                "The early bird catches the worm.",
  22:                "The pen is mightier than the sword." 
  23:              };
24: var result = from sentence in text // The sentence range variable. The text array contains
25: let words = sentence. Split ('') // one piece of sentence is separated into one word by Split ('') and the result is stored in the words variable through "let ".
26: from word in words // The range variable word, which extracts a word from words.
27: let v = word. ToLower () // a word is converted by ToLower () and stored in another range variable v through "let ".
28: where v [0] = 'A' | v [0] = 'E' | v [0] = 'I' | v [0] = 'o' | v [0] = 'U' // filter each v variable (array) through the where key) the first element in is the "vowel" element.
29: select word; // word: entire word
  30:              foreach (var i in result)
  31:              {
32: Console. WriteLine ("\" {0} \ "starting with a vowel", I );
  33:              }
  34:              Console.ReadKey();
  35:   
  36:          }
  37:      }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.