C# Linq 文档(三)

目录

概述

十三、Sum / Min / Max / Average

十四、Distinct

十五、Concat

十六、Join

十七、ToList 

十八、ToArray

十九、ToDictionary


C# Linq 文档(一)
1.Where,2.Select,3.GroupBy,4.First / FirstOrDefault,5.Last / LastOrDefault

C# Linq 文档(二)
1.OrderBy ,2.OrderByDescending,3.Skip,4.Take,5.Any,6.All

C# Linq 文档(三)
1.Sum / Min / Max / Average,2.Distinct,3.Concat,4.Join,5.ToList ,6.ToArray,7.ToDictionary

C# Linq 文档(四)
1.SelectMany,2.Aggregate,3.DistinctBy,4.Reverse,5.SequenceEqual,6.Zip,7.SkipWhile ,8.TakeWhile

C# Linq 文档(一)_熊思宇的博客-CSDN博客

C# Linq 文档(二)_熊思宇的博客-CSDN博客

C# Linq 文档(四)_熊思宇的博客-CSDN博客


概述

语言集成查询 (LINQ) 是一系列直接将查询功能集成到 C# 语言的技术统称。 数据查询历来都表示为简单的字符串,没有编译时类型检查或 IntelliSense 支持。 此外,需要针对每种类型的数据源了解不同的查询语言:SQL 数据库、XML 文档、各种 Web 服务等。 借助 LINQ,查询成为了最高级的语言构造,就像类、方法和事件一样。

对于编写查询的开发者来说,LINQ 最明显的“语言集成”部分就是查询表达式。 查询表达式采用声明性查询语法编写而成。 使用查询语法,可以用最少的代码对数据源执行筛选、排序和分组操作。 可使用相同的基本查询表达式模式来查询和转换 SQL 数据库、ADO .NET 数据集、XML 文档和流以及 .NET 集合中的数据。
 

十三、Sum / Min / Max / Average

计算序列中指定属性的总和、最小值、最大值、平均值。

using System;
using System.Collections.Generic;
using System.Linq;namespace LinqTest
{internal class Program{static void Main(string[] args){List numList = new List { 1, 4, 5, 24, 43, 67, 12, 90, 15 };int sum = numList.Sum();Console.WriteLine("总和:{0}", sum);int min = numList.Min();Console.WriteLine("最小值:{0}", min);int max = numList.Max();Console.WriteLine("最大值:{0}", max);double average = numList.Average();Console.WriteLine("平均值:{0}", average);Console.ReadKey();}}
}

运行:

十四、Distinct

Distinct 方法用于从集合中筛选出不重复的元素。它返回一个新的集合,其中包含原始集合中的唯一元素。

using System;
using System.Collections.Generic;
using System.Linq;namespace LinqTest
{internal class Program{static void Main(string[] args){int[] numList = { 1, 2, 2, 3, 3, 4, 5, 5 };var distinctNumbers = numList.Distinct();foreach (var number in distinctNumbers){Console.WriteLine(number);}Console.ReadKey();}}
}

运行:

十五、Concat

Concat 方法用于将两个集合合并为一个新的集合。当前方法没有重载函数,通常用在将数组分类后,挑出需要的集合,合并后重新处理。

using System;
using System.Collections.Generic;
using System.Linq;namespace LinqTest
{internal class Program{static void Main(string[] args){int[] numbers1 = { 1, 2, 3 };int[] numbers2 = { 4, 5, 6 };var mergedNumbers = numbers1.Concat(numbers2);foreach (var number in mergedNumbers){Console.WriteLine(number);}Console.ReadKey();}}
}

运行:

十六、Join

根据两个序列中的关联键,将它们的元素进行匹配。

Join 的用法,在 Linq 中算是比较复杂的了,用起来变化也比较多,这里用一个简单的例子,推荐各位多写多练,多写一些案例,用多了就比较熟了

using System;
using System.Collections.Generic;
using System.Linq;namespace LinqTest
{internal class Program{static void Main(string[] args){List students1 = new List{new Student { Id = 1, Name = "柱子" },new Student { Id = 2, Name = "李四" },new Student { Id = 3, Name = "铁蛋" }};List students2 = new List{new Student { Id = 1, Name = "张三" },new Student { Id = 3, Name = "狗剩" },new Student { Id = 5, Name = "二狗" }};var joinedStudents = students1.Join(students2,s1 => s1.Id,s2 => s2.Id,(s1, s2) => new { Name1 = s1.Name, Name2 = s2.Name });foreach (var student in joinedStudents){Console.WriteLine($"Name1: {student.Name1}, Name2: {student.Name2}");}Console.ReadKey();}}class Student{public int Id { get; set; }public string Name { get; set; }}
}

运行:

在上面 Join 的代码中,首先看下面这两句代码,这里是在两个 List 中,找出一个共同的键,比如 s1 和 s2 都有一个共同的键 Id,这里会判断 Id 的值在两个 List 中是否有相同的值,它查询的结果同样是个集合,在上面代码可以看到,Id 有两个共同的值,1 和 3。

s1 => s1.Id,
s2 => s2.Id,

下面代码是一个 Lambda 表达式,它将 s1 和 s2 作为参数传递到了一个新的对象中,Name1 和 Name2 都是自己定义的字段名,这里可以随意取名字

(s1, s2) => new 
{ Name1 = s1.Name, Name2 = s2.Name 
});

比如,取名成这样,也是不会报错的

找出相同字段的集合后,就可以在这个 Lambda 表达式里重新筛选数据了,我们把鼠标放到 joinedStudents 这里,可以看到是一个叫 'a 的类,这就是上面我们自定义的类

十七、ToList 

将序列转换为List

在上面的很多案例都可以将查询的结果转换为 List,下面来看一个案例,非常简单

using System;
using System.Collections.Generic;
using System.Linq;namespace LinqTest
{internal class Program{static void Main(string[] args){List numList = new List { 1, 4, 5, 24, 43, 67, 12, 90, 15 };List list = numList.Where(x => x > 40).ToList();foreach (int num in list){Console.WriteLine(num);}Console.ReadKey();}}
}

运行:

十八、ToArray

将序列转换为Array

ToArray 和 ToList 用法差不多的,只是转换的结果不一样

using System;
using System.Collections.Generic;
using System.Linq;namespace LinqTest
{internal class Program{static void Main(string[] args){List numList = new List { 1, 4, 5, 24, 43, 67, 12, 90, 15 };int[] ints = numList.Where(x => x > 40).ToArray();foreach (int num in ints){Console.WriteLine(num);}Console.ReadKey();}}
}

运行:

十九、ToDictionary

根据指定的键选择器,将序列转换为字典。

using System;
using System.Collections.Generic;
using System.Linq;namespace LinqTest
{internal class Program{static void Main(string[] args){List peopleList = new List(){new People(){Name="张三", Age=12, Career="学生" },new People(){Name="柱子", Age=25, Career="农民" },new People(){Name="铁蛋", Age=23, Career="农民" },new People(){Name="狗剩", Age=34, Career="职员" },new People(){Name="二狗", Age=28, Career="职员" },};Dictionary peopleDictionary = peopleList.ToDictionary(x => x.Name);foreach (var kvp in peopleDictionary){Console.WriteLine($"Key: {kvp.Key}, Value.Age: {kvp.Value.Age}");}Console.ReadKey();}}class People{public string Name { get; set; }public int Age { get; set; }public string Career { get; set; }}
}

运行:

end


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部