One, standard query operators
1, C # provides standard query operators, such as I want to choose a patent series (pantents) with the beginning of the year 19 patent, you can use the following statement:
ienumerable<patent> pantentwhere = pantents. Where (pantent = pantent. Yearofpublicaton.startswith ("+"));
Of course, the statement here simply defines the query, at which point the Pantentwhere does not have the content, the query specified by the lambda expression is not executed, and the query rule is executed only when the Pantentwhere collection is traversed, which is the "deferred execution" of standard queries in C #
2. Projection
The patent class contains the name of the year the application number of the inventor, etc., if I want to the patent class in the collection of each patent type to include only the name and year of the type, then you can use Select to do, the code is as follows:
1 var pantentselect "= Pantents. Select ( 2 pantent = 3 return new 5 { 6 Title = Pantent. Title, 7 year = Pantent. Yearofpublicaton 8 }; 9 });
As you can see, the lambda expression returns a type that contains the name and year. When traversing Pantentselect, its projection statement executes, which is a collection of [(name, value), (year, value)].
3. Sorting
With the standard query operator orderbydescending and thenbydescending, you can do multiple-condition sorting with the following code:
1 2 pantent. Yearofpublicaton). ThenByDescending (3 pantent = pantent. Title);
As you can see, only one order is used, and it gets and only gets a parameter that becomes keyselector, such as the Yearofpublicaton in this example. If you want to continue to sort by the second keyword, you can only use ThenBy to execute on the basis of the order. The use of multiple order in a row will only undo the last order, so use ThenBy instead of continue to use.
Here are just a few items to list, because if you perform more complex queries and projections, you will produce more cumbersome code. As a result, standard query expressions are introduced in C # 3.0, which is more like the SQL language
Ii. Standard query expressions
1, simple example, the next section of the code to complete the function is to retrieve the word does not contain *:
1 class Program2 {3 Static string[] Keywords = {"*a","*b","*c","*d","*e","*f","a","b","C","D","e","F","g","h","I"};4 Static voidMain (string[] args)5 {6 ShowContextualKeyword1 ();7 }8 Public Static voidShowContextualKeyword1 ()9 {Tenienumerable<string> selection = fromWordinchKeywords One where!word. Contains ('*') A SelectWord; - foreach(stringSinchselection) - { theConsole.WriteLine (" "+s); - } - } -}
It is worth detailing the type inference: The select casts back a collection of word, the word type is the word that follows from, and is inferred from keywords. Keywords is a collection of strings, so word is of type string, so select projects to Ienumerable<string>
2, change the return type.
The select can return not only the original type, but also the specified type, and I personally summarize that he will return the collection type of the variable following the select.
The following code, instead of the collection of filename, returns the collection of FileInfo:
1 Public Static voidList1 (stringRootDirectory,stringsearchpattern)2 {3ienumerable<fileinfo> files = fromFileNameinchDirectory.GetFiles (rootdirectory, searchpattern)4 Select NewFileInfo (fileName);5 foreach(FileInfo fileinchfiles)6 {7Console.WriteLine (". {0} ({1})", file. Name,file. LastWriteTime);8 }9}
C # standard query expressions