某某茶叶有限公司欢迎您!
金沙棋牌在线 > 金沙棋牌在线 > Linq在Array,【金沙棋牌在线】List,Dictionary中的应用

Linq在Array,【金沙棋牌在线】List,Dictionary中的应用

时间:2020-01-17 12:54

假如我们有一个列表

Linq在Array,List,Dictionary中的应用

今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下:

金沙棋牌在线 1金沙棋牌在线 2

 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 Program
10     {
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,2,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

金沙棋牌在线 3

 

public class County { public int CountyId { get; set; } public string CountName { get; set; } } List<County> counties = new List<County>(); counties.Add(new County() { CountyId = 1, CountName = "北京" }); counties.Add(new County() { CountyId = 2, CountName = "上海" }); counties.Add(new County() { CountyId = 3, CountName = "广州" });

Linq与Lambda,神一般的工作效

要查找 CountyId == 2 对应的 County,方法有很多。

 

一、最传统方法:

   通过对linq和lambda的学习,越发感觉linq和lambda的重要性,他们能极大地简化程序,同时提升程序的可读性,大大提升了我们的工作效率,在公司的招聘要求中也是很常见的,也是.net菜鸟与合格开发人员的区别。通过以下练习,可以巩固学习到的linq和lambda知识,加深自己对linq和lambda的理解。

for (int i = 0; i < counties.Count; i++) { if (counties[i].CountyId == 2) { county = counties[i]; break; } }

 

金沙棋牌在线,二、稍微简单点的方法:

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6  
 7 namespace linq
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //List<int> arry = new List<int>() { 3, 4, 1, 3, 33, 5, 23, 45, 2, 49, 100, 34 };
14             //1、查询出所有大于30的元素集合,并显示出来
15             //var value = from u in arry
16             //            let n = Convert.ToInt32(u)
17             //            where u > 30
18             //            select u;
19  
20             //2、将arry进行降序排列,并显示出来
21             //var value1 = arry.OrderByDescending(i => i);  //lambda表达式
22  
23             //var value2 = from u in arry
24             //             orderby u descending
25             //             select u;
26  
27             List<User> users = new List<User>() { new User(20, "Name20"), new User(4, "CK4"), new User(79, "Name79"), new User(52, "CD52") };
28  
29             //1、查询出所有users中的Name,并显示出来
30  
31             //var value = from u in users
32             //            select u.Name;
33  
34             //var value1 = users.Select(u => u.Name); //lambda表达式
35  
36             //2、查询出ID在List<int> ids = new List<int>() {79,20};中的用户,并显示出ID,Name
37             List<int> ids = new List<int>() { 79, 20 };
38             //var value = from u in users
39             //            where ids.Any(i => i == u.ID)
40             //            select u;
41  
42             //var value = from u in users
43             //            where ids.Select(c => c).Contains(u.ID)
44             //            select u;
45  
46             //3、查询出所有Name中含有“C”的user,并显示出ID,Name
47             var value = from u in users
48                         where u.Name.Contains("C")
49                         select u;
50  
51  
52             foreach (var i in value)
53             {
54                 Console.WriteLine(i.ID);
55                 Console.WriteLine(i.Name);
56             }
57  
58             Console.ReadKey();
59                
60         }
61  
62  
63         public class User
64         {
65             public int ID { get; set; }
66             public string Name { get; set; }
67  
68             public User(int id, string name)
69             {
70                 ID = id;
71                 Name = name;
72             }
73         }
74     }
75 }

foreach (var item in counties) { if (item.CountyId == 2) { county = item; break; } }

 

三、直接委托的写法: