c# linq基本用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace linq2
{
    class Employee
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public bool Gender { get; set; }
        public int Salary { get; set; }
        public override string ToString()
        {
            return $"Id={Id},Name={Name},Age={Age},Gender={Gender},Salary={Salary}";
        }
    }
    class SamplePerson
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> employees = new List<Employee>()
            {
                new Employee {Id=1,Name="jerry",Age=28,Gender=true,Salary=5000},
                new Employee {Id=2,Name="jim",Age=33,Gender=true,Salary=3000},
                new Employee {Id=3,Name="lily",Age=35,Gender=false,Salary=9000},
                new Employee {Id=4,Name="luck",Age=16,Gender=false,Salary=2000},
                new Employee {Id=5,Name="kimi",Age=25,Gender=true,Salary=1000},
                new Employee {Id=6,Name="nancy",Age=35,Gender=false,Salary=8000},
                new Employee {Id=7,Name="zack",Age=35,Gender=true,Salary=8500},
                new Employee {Id=8,Name="jack",Age=35,Gender=true,Salary=8000},
            };
 
            //where 符合条件的返回
            IEnumerable<Employee> employeesWgere =  employees.Where(e => e.Age > 30);
            //SingleOrDefault 返回唯一,如果不是唯一返回默认值,对象为null
            Employee employeeSingalOrDefault = employees.SingleOrDefault(e => e.Name == "luck");
            //firstOrDefault 默认取第一个符合条件的值,如果没有返回默认值,对象为null
            Employee employeefirstDefault = employees.FirstOrDefault(e => e.Name == "lily");
 
            //group max average count
            IEnumerable<IGrouping<int,Employee>> result =  employees.GroupBy(e => e.Age);
            foreach (var g in result)
            {
                Console.WriteLine($"年龄分组:{g.Key}");
                Console.WriteLine($"人数:{g.Count()}");
                Console.WriteLine($"最大工资:{g.Max(e => e.Salary)}");
                Console.WriteLine($"平均工资:{g.Average(e => e.Salary)}");
                Console.WriteLine("*************************************");
            };
            //select 映射
            IEnumerable<SamplePerson> samplePeoples =  employees.Select(e => new SamplePerson {
                Id = e.Id, Name = e.Name,Gender = e.Gender?"男":"女"}
            );
            //select 结合 GroupBy
            var resultGroup = employees.GroupBy(e => e.Age)
                .Select(
                g =>
                new { Key = g.Key,
                    Nums = g.Count(),
                    AverAge=g.Average(e=>e.Salary),
                    MaxResult = g.Max(e => e.Salary
                    )});
 
            //例子:获取id>2,按age分组,根据age排序,获取前3条数据,映射匿名对象存储年龄,人数,平均工资
            // where groupby orderby take select
            var result5 = employees.Where(
                e => e.Id > 2)
                .GroupBy(e => e.Age)
                .OrderBy(g=>g.Key)
                .Take(3)
                .Select(g => new { Age = g.Key, Nums = g.Count(), Avg = g.Average(e=>e.Salary) });
 
            //查询语法
            var selectlist = from e in employees
                             where e.Salary > 3000 && e.Salary < 8000
                             orderby e.Age
                             select new { e.Name, e.Age, Gender = e.Gender ? "男" : "女" };
 
            //分割字符串求平均值
            string numbers = "61,90,100,99,18,22,38,66,80,93,55,50,89";
            var avgResult = numbers.Split(",").Select(n => Convert.ToInt32(n)).Average();
 
            //统计字符串中每个字母出现的次数,忽略大小写,从高到底排序,次数必须大于2
            string s = "hellooo World Haihhh ,hh";
            var LetterResult  = s.Where(s => char.IsLetter(s))
                .Select(s => char.ToLower(s))
                .OrderByDescending(s => s)
                .GroupBy(s => s)
                .Select(g => new { g.Key, Count = g.Count() })
                .Where(g => g.Count > 2);
            foreach(var letter in LetterResult)
            {
                Console.WriteLine(letter);
            };
        }
    }
}

 今天看了b站上杨中科老师说的c# linq教程,感觉很有意思,手动撸了一遍。

posted on   金焱_Huang  阅读(67)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示