Applications of Linq in Array, List, and Dictionary, linqdictionary

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.