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: }