Applications of Linq in Array, List, and Dictionary, linqdictionary
Applications of Linq in Array, List, and Dictionary
In actual work today, we need to sort array, list, and dictionary, and try linq. The Code is as follows:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. text. regularExpressions; 6 7 namespace Test 8 {9 class Program10 {11 static void Main (string [] args) 12 {13 Console. writeLine ("Hello world! "); 14 string inputStr =" abf dcf frgt "; 15 inputStr = Regex. replace (inputStr. trim (), "{2,}", ""); 16 Console. writeLine (inputStr); 17 18 int [] num = {1, 2, 3, 4, 5, 5, 6, 1, 3, 2}; 19 // Array. sort (num); 20 var nums = (from l in num orderby l select l ). toArray (); 21 Console. write ("Array: \ n"); 22 foreach (int I in nums) 23 Console. write (I. toString (); 24 Console. write ("\ nList: \ n"); 25 List <int> list = new List <int> () {1, 1, 9, 3, 2, 7 }; 26 list = (from l in list orderby l select l ). toList (); 27 foreach (var l in list) 28 Console. write (l); 29 Dictionary <int, int> counts = new Dictionary <int, int> () {1, 2}, {2, 3}, {3, 1 }}; 30 // var results = (from d in counts orderby d. value descending select d ). take (2 ). toDictionary (k => k. key, v => v. value); 31 var results = (from d in counts orderby d. value descending select d. key ). take (2 ). toArray (); 32 Console. write ("\ nDictionary: \ n"); 33 foreach (var d in results) 34 Console. writeLine (d); 35 36 Console. readKey (); 37} 38} 39}View Code