1. Application scenarios
In query expressions, storing the results of subexpressions is sometimes useful, so that it can be used in subsequent clauses. This can be done using 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. But if the range variable stores a queryable type, it can be queried.
2. Sample code
using System; using ; namespace UseLet { class Program { 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." }; var earlyBirdQuery = from sentence in strings let words = (' ') from word in words let w = () where w[0] == 'a' || w[0] == 'e' || w[0] == 'i' || w[0] == 'o' || w[0] == 'u' select word; foreach (var v in earlyBirdQuery) { ("\"{0}\" starts with a vowel", v); } ("Press any key to exit"); (); } } }
From the above effects, we can see the function of the clause let. If you do not use let, you must call ToLower in each predicate of the where clause, and let can save variables in the from statement to use.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.