Standard query operator and standard query operator
The emergence of Linq makes the code much simpler. It was basically used in the project before, but it has not been fully organized. Today, we will use it this weekend to facilitate later use of it. Linq operations can be divided into aggregation, join, conversion, element operators, equal operations, generation, grouping, division, projection, quantity, filtering, sorting, etc, let's talk about it one by one through the example.
Before a specific instance, define two sets for the following three console output methods:
1 List<string> words = new List<string>() { "zero", "one", "two", "three", "four" };2 List<int> numbers = new List<int>() { 0, 1, 2, 3, 4 };
1 static void Write(string name, object obj) 2 { 3 Console.WriteLine($"method:{name},\t result:{obj.ToString()}"); 4 } 5 6 static void Write(string title) 7 { 8 Console.WriteLine($"---------{title}-------------"); 9 }10 11 static void Write()12 {13 Console.WriteLine($"-------------------------------");14 }
Aggregation
The most common Aggregate operator is Aggregate. All other Aggregate operators can be expressed as calls to Aggregate,All Aggregate operators use the immediate execution mode.
1 Write ("aggregate operation"); 2 int sum = numbers. sum (); 3 Write ("sum", sum); 4 int count = numbers. count; 5 Write ("count", count); 6 double average = numbers. average (); 7 Write ("average", average); 8 long longCount = numbers. longCount (x => x % 2 = 0); 9 Write ("longCount", longCount); 10 int min = words. min (p => p. length); 11 Write ("min", min); 12 int max = words. max (p => p. length); 13 Write ("max", max); 14 string aggregate = numbers. aggregate ("seed", (current, item) => current + item, result => result. toUpper (); 15 Write ("aggregate", aggregate); 16 Write ();
Result:
Connection
1 Write ("connection"); 2 List <int> concat = numbers. concat (new List <int> () {2, 3, 4, 5, 6 }). toList (); 3 Write ("concat", string. join (",", concat); 4 Write ();
Result:
Conversion
The meanings of toArray and toList are obvious. They read the entire sequence into the memory and return the result as an array or a List <T>. Both are executed immediately. cast and ofType convert an untyped sequence to a typed sequence or throw an exception (for cast ), or ignore input sequence elements that cannot be implicitly converted to the sequence element type (for ofType), this operator can also be used to convert a typed sequence to a more specific type sequence. The conversion is delayed in the form of a stream. Both ToDictionary and TolookUp use a delegate to obtain the keys of any specific element. The execution mode is immediate execution.
1 Write ("convert"); 2 List <Object> allString = new List <Object> () {"These", "are", "all", "strings "}; 3 List <Object> notAllString = new List <Object> () {"Number", "at", "the", "end", 5 }; 4 5 IEnumerable <string> castOne = allString. cast <string> (); 6 Write ("castOne", string. join (",", castOne); 7 IEnumerable <string> ofTypeOne = allString. ofType <string> (); 8 Write ("ofTypeOne", string. join (",", ofTypeOne); 9 10 IEnumerable <string> castTwo = notAllString. cast <string> (); 11 Write ("castOne", string. join (",", castTwo); // If the conversion fails, an exception 12 IEnumerable <string> ofTypeTwo = notAllString is thrown. ofType <string> (); 13 Write ("ofTypeOne", string. join (",", ofTypeTwo); 14 15 int [] toArray = numbers. toArray (); 16 List <int> toList = numbers. toList (); 17 18 Dictionary <string, string> dictionary = words. toDictionary (w => w. substring (0, 2); 19 ILookup <char, string> toLookUp = words. toLookup (word => word [0]); 20 21 IDictionary <char, string> toDictionary = words. toDictionary (p => p [0]); // exception ();
Element Operators
Each operator is executed in the same way. Selecting a simplified version of a single element is to return a specific element if it exists. If it does not exist, an exception is thrown, and a version ending with orDefault is also displayed, the functions of the orDefault version are the same, but the expected element is not found. The default value of the returned result type is not thrown, but there is an exception. If the sequence is empty, singleDefault returns the default value, but if there are more than one element in the sequence, an exception is thrown, just like Single ..All operators use the immediate execution mode.
1 var elementAt = words. elementAt (2); // "Two" 2 var elementAtOrDefault = words. elementAtOrDefault (10); // Null 3 var first = words. first (); // Zero 4 var firstTwo = words. first (p => p. length = 3); // one 5 // var firstThree = words. first (p => p. length = 10); // exception 6 var firstOrDefault = words. firstOrDefault (p => p. length = 10); // Null 7 var last = words. last (); // four 8 var single = words. single (); // exception. More than one element 9 var singleDefault = words. singleOrDefault (); // exception. More than one element is 10 var singleTwo = words. single (p => p. length = 5); // three11 var singleThree = words. single (p => p. length = 10); // no matching element 12 var singleDefaultTwo = words. singleOrDefault (P => P. length = 10); // Nul
Equal operation
Compare the elements in the two sequences one by one in order to see if they are equal. The execution mode is instant execution.
1 bool sequenceEqual = words.SequenceEqual(new List<string>() { "zero", "one", "two", "three", "four" });//true2 bool sequenceEqualTwo = words.SequenceEqual(new List<string>() { "ZERO", "OME", "TWO", "THREE", "FOUR" });//false3 bool sequenceEqualThree = words.SequenceEqual(new List<string>() { "ZERO", "OME", "TWO", "THREE", "FOUR" }, StringComparer.OrdinalIgnoreCase);//true
Generate
Among all the generation operators, only one will process the existing sequence: DefaultIfEmpty. If the sequence is not empty, the original sequence will be returned; otherwise, the sequence containing but not containing elements will be returned. The elements are generally sequential.Default value, but the overload method allows you to set the value to be used. All generated operators are executed with delay and the results are streamed. That is to say, they do not generate a collection in advance and return it. However, return the correct typeThe empty method of an empty array is an exception. An empty array is completely immutable. Therefore, all such calls of all element types of the same element type will return the same empty array.
1 var defaultIfEmpty = numbers. defaultIfEmpty (); // 0, 1, 2, 3, 42 var defaultIfEmptyTwo = new int [0]. defaultIfEmpty (); // 03 var defaultIfEmptyThree = new int [0]. defaultIfEmpty (10); // 04 var range = Enumerable. range (15, 2); // 15,165 var repeat = Enumerable. repeat (25, 2); // 25,256 IEnumerable <int> empty = Enumerable. empty <int> (); // an Empty sequence of IEnumerable <int> type
Group
1 IEnumerable<IGrouping<int, string>> groupby = words.GroupBy(p => p.Length);2 IEnumerable<IGrouping<int, string>> groupTwo = words.GroupBy(p => p.Length, p => p.ToUpper());3 IEnumerable<string> groupThree = words.GroupBy(p => p.Length, (key, g) => key + ":" + g.Count());
Result:
Projection
Select is a simple one-to-one projection from the source element to the result element. selectMany is used when the query expression contains multiple from clauses. Each element in the original sequence is used to generate a new sequence. Both projections are
Delayed execution,. Net 4 introduces a new operator zip. It contains two sequences and applies the specified projection to each element: first, the first element of each sequence, then the second element of each sequence, and so on.
When any source sequence reaches the end, the result sequence is stopped.
1 IEnumerable<int> selectOne = words.Select(p => p.Length);//4,3,3,5,42 IEnumerable<string> selectTwo = words.Select((word, index) => index.ToString() + ":" + word);//0:zero,1:one ....3 IEnumerable<char> selectMany = words.SelectMany(p => p.ToCharArray());//z,e,r,o,o...4 IEnumerable<string> selectManyTwo = words.SelectMany((word, index) => Enumerable.Repeat(word, index));5 IEnumerable<string> zip = names.Zip(colors, (x, y) => x + "-" + y);
Quantity
The number operator returns a boolean value, which is executed immediately.
1 bool allOne = words.All(p => p.Length > 3);//false2 bool allTwo = words.All(p => p.Length > 2);//true3 bool any = words.Any();//true4 bool anyTwo = words.Any(p => p.Length == 6);//false5 bool antThree = words.Any(p => p.Length == 5);//true6 bool contains = words.Contains("FOUR");//false7 bool containsTwo = words.Contains("FOUR", StringComparer.OrdinalIgnoreCase);//true
Filter
Two filter operators: where and ofType. Where returns a sequence, where always uses delayed execution and streaming data.
1 IEnumerable<string> where = words.Where(p => p.Length > 3);//zero,three,four2 IEnumerable<string> whereTwo = words.Where((word, index) => index < word.Length);//one,two,three
Set-Based Operators
It is natural to use two sequences as a set of elements. Each of the four geometric-based operators has two overload methods. One is equivalent to the default comparison of element types, and the other is used to specify the comparison in additional parameters.All set operators are delayed.
1 List <string> abbc = new List <string> () {"a", "B", "B", "c "}; 2 List <string> cd = new List <string> () {"c", "d"}; 3 4 var distinct = abbc. distinct (); // a, B, c5 var intersect = abbc. intersect (cd); // c intersection 6 var union = abbc. union (cd); // a, B, c, d // Union 7 var except Ct = abbc. except (cd); // a, B // difference set 8 var exceptTwo = cd. t (abbc); // d
Sort
OrderBy and OrderByDescending provide major sorting methods, while thenBy and ThenByDescending provide secondary sorting methods. It is used to differentiate elements that cannot be differentiated using the main sorting method. In eachIn this case, both the projection from element to sort key and the comparison between keys must be specified. The sorting by linq is relatively stable, that is, if the two elements are considered equal according to their sorting keywords, they will followSequential return
1 var orderBy = words.OrderBy(p => p);//four,one,three,two,zero2 var orderByTwo = words.OrderBy(p => p[1]);//zero,three,one,four,two3 var orderByThree = words.OrderBy(p => p.Length);//one,two,zero,four,three4 var orderByDescending = words.OrderByDescending(p => p.Length);//three,zero,four,one,two5 var orderByFour = words.OrderBy(p => p.Length).ThenBy(p => p);//one,two,four,zero,three6 var orderByFive = words.OrderBy(p => p.Length).ThenByDescending(p => p);//two,one,zero,four,three7 words.Reverse();//four,three,two,one,zero