Linq是C# 最强语法之一,和委托,get set并列(在我的心中)。我很早就听说了Linq 很牛了,这里来了解一下Linq如何写代码
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 == 0
select item1;
Console.WriteLine(JsonConvert.SerializeObject(numLinq));
//[0,2,4,6]
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 numbers
where item1 + item2 == 4
select 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 numbers
where item1 + item2 == 4
select (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}]
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 ascending
select item1;
Console.WriteLine(JsonConvert.SerializeObject(numLinq));
//降序
var numLinq2 = from item1 in numbers
orderby item1 descending
select item1;
Console.WriteLine(JsonConvert.SerializeObject(numLinq2));
Console.WriteLine("Hello, World!");
Console.ReadKey();
}
我们这里新建一个Person类
public class Person
{
public string Name { get; set; }
public int Id { get; set; }
}
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 persons
group item by item.Id;
Console.WriteLine(JsonConvert.SerializeObject(res));
Console.WriteLine("Hello, World!");
Console.ReadKey();
}
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 person1
join item2 in person2 on item1.Id equals item2.Id
select new Person() { Name = item1.Name+item2.Name,Id = item1.Id};
Console.WriteLine(JsonConvert.SerializeObject(res));
Console.WriteLine("Hello, World!");
Console.ReadKey();
}
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();
}
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();
}
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();
}
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,如果想详细了解,可以去看看这个文章,写的确实是非常的全面