Example of a let sentence application in C #

Source: Internet
Author: User
This article mainly introduces the let words in C #, the article through the application examples introduced in detail, I believe that we have a certain reference value, the need for friends below to see it together.

First, the application scenario

In query expressions, it is sometimes useful to store the results of a subexpression so that it can be used in subsequent clauses. You can do this with the Let keyword, which creates a new scope variable and initializes the variable with the result of the expression you provide. Once the range variable is initialized with a value, it cannot be used to store other values. However, if the range variable stores a queryable type, it can be queried.

Second, the sample code

Using system;using System.linq;namespace uselet{class Program {static void Main () {  string[] strings = {  "A Penn Y saved is a penny earned. ",  " the early bird catches the worm. ",  " the pen is mightier than the sword. "  };  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;  foreach (Var v in earlybirdquery)  {  Console.WriteLine ("\" {0}\ "starts with a vowel", v);  }  Console.WriteLine ("Press any key to exit");  Console.ReadLine (); } }}

The effect of clause let can be seen from the above effects. If let is not used, you must call ToLower in each predicate of the WHERE clause, and let can save the variables in the from sentence for use.

Summarize

Above is the content of the let sentence application example in C #, more about topic.alibabacloud.com (www.php.cn)!



  • Related Article

    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.