文章目录
- 前言
- 简单使用:能被2整除
- ValueTuple使用:两数相加等于4
- 不使用元祖
- 使用元祖
- 排序
- 基于类的Linq
- GroupBy
- Join
- Distinct去重
- 普通去重
- 选择去重
- 集合去重
- Except
- Intersect
- Union
- 总结
前言
Linq是C# 最强语法之一,和委托,get set并列(在我的心中)。我很早就听说了Linq 很牛了,这里来了解一下Linq如何写代码
C# Linq详解
C# 元祖,最佳的临时变量。
简单使用:能被2整除
static void Main(string[] args)
{int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };//选择能被2整除的数var numLinq = from item1 in numbers where item1 %2 == 0select item1;Console.WriteLine(JsonConvert.SerializeObject(numLinq));//[0,2,4,6]Console.WriteLine("Hello, World!");Console.ReadKey();
}
这里简单说明一下基础语法:
- from item in source:item临时变量,source数据源
- where:临时变量成立条件
- select:返回值
ValueTuple使用:两数相加等于4
不使用元祖
static void Main(string[] args){int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };//选择两数相加等于4的var numLinq = from item1 in numbers from item2 in numberswhere item1 + item2 == 4select new int[] { item1,item2};Console.WriteLine(JsonConvert.SerializeObject(numLinq));Console.WriteLine("Hello, World!");Console.ReadKey();}
使用元祖
static void Main(string[] args){int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };//选择两数相加等于4的var numLinq = from item1 in numbers from item2 in numberswhere item1 + item2 == 4select (item1,item2);Console.WriteLine(JsonConvert.SerializeObject(numLinq));Console.WriteLine("Hello, World!");Console.ReadKey();}
结果:
[{"Item1":0,"Item2":4},{"Item1":1,"Item2":3},{"Item1":2,"Item2":2},{"Item1":3,"Item2":1},{"Item1":4,"Item2":0}]
排序
- orderby ascending:升序
- orderby descending:降序
static void Main(string[] args)
{int[] numbers = new int[] { 1,6,4,0,3,4,6,7,5 };//升序var numLinq = from item1 in numbers orderby item1 ascendingselect item1;Console.WriteLine(JsonConvert.SerializeObject(numLinq));//降序var numLinq2 = from item1 in numbersorderby item1 descendingselect item1;Console.WriteLine(JsonConvert.SerializeObject(numLinq2));Console.WriteLine("Hello, World!");Console.ReadKey();
}
基于类的Linq
我们这里新建一个Person类
public class Person{public string Name { get; set; }public int Id { get; set; }}
GroupBy
static void Main(string[] args)
{var persons = new List<Person>() {new Person(){Name = "小王",Id = 1},new Person(){Name = "小红",Id=2},new Person(){Name = "小黑", Id=2},};//使用聚合函数,返回的是分类好的数组var res = from item in personsgroup item by item.Id;Console.WriteLine(JsonConvert.SerializeObject(res));Console.WriteLine("Hello, World!");Console.ReadKey();
}
Join
join + in是两集合聚合,聚合条件必须要用equals,不能用=
static void Main(string[] args)
{var person1 = new List<Person>() {new Person(){Name = "小王",Id = 1},new Person(){Name = "小红",Id=2},};var person2 = new List<Person>() {new Person(){Name = "小拉",Id = 1},new Person(){Name = "小辣",Id=2},};//将相同ID的名称拼接var res = from item1 in person1join item2 in person2 on item1.Id equals item2.Idselect new Person() { Name = item1.Name+item2.Name,Id = item1.Id};Console.WriteLine(JsonConvert.SerializeObject(res));Console.WriteLine("Hello, World!");Console.ReadKey();
}
Distinct去重
普通去重
static void Main(string[] args){var lists = new List<int>() {1,2,3,4,5,5,5,6,1,2 };//普通去重var res = from item in lists.Distinct() select item; Console.WriteLine(JsonConvert.SerializeObject(res));Console.WriteLine("Hello, World!");Console.ReadKey();}
选择去重
static void Main(string[] args){var lists = new List<Person>() {new Person(){Name= "小王",Id= 1},new Person(){Name = "小丽" ,Id=2},new Person(){Name = "小乐",Id = 2},};//普通去重var res = from item in lists.DistinctBy(x => x.Id)select item;Console.WriteLine(JsonConvert.SerializeObject(res));Console.WriteLine("Hello, World!");Console.ReadKey();}
集合去重
Except
static void Main(string[] args)
{var list1 = new int[] { 1, 2, 3, 4, 5 };var list2 = new int[] { 3, 4, 5, 6, 7 };var res = from item in list1.Except(list2).ToList()select item;Console.WriteLine("list1去掉list2重复的");Console.WriteLine(JsonConvert.SerializeObject(res));Console.WriteLine("Hello, World!");Console.ReadKey();
}
Intersect
static void Main(string[] args)
{var list1 = new int[] { 1, 2, 3, 4, 5 };var list2 = new int[] { 3, 4, 5, 6, 7 };var res = from item in list1.Intersect(list2)select item;Console.WriteLine("list1拿到list2重复的");Console.WriteLine(JsonConvert.SerializeObject(res));Console.WriteLine("Hello, World!");Console.ReadKey();
}
Union
static void Main(string[] args)
{var list1 = new int[] { 1, 2, 3, 4, 5 };var list2 = new int[] { 3, 4, 5, 6, 7 };var res = from item in list1.Union(list2)select item;Console.WriteLine("list1和list2的并集");Console.WriteLine(JsonConvert.SerializeObject(res));Console.WriteLine("Hello, World!");Console.ReadKey();
}
总结
我这里只是简单的使用了一下Linq,如果想详细了解,可以去看看这个文章,写的确实是非常的全面
C# Linq详解