一:什么是LINQ

LINQ代表语言集成查询,是.net框架的扩展,它允许我们用SQL查询数据库的方式来查询数据的集合


二:LINQ延迟查询的特性

延迟查询是指查询操作并不是在定义的时候执行,而是在遍历集合中的数据时才执行

因为作为yield迭代器的主体,只有使用foreach遍历执行到MoveNext时才会真正执行方法

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
var list = personList.Where((p) => p.Age < 30);

foreach (var temp in list)
{
Console.WriteLine(temp);
}

personList.Add(new Person() { Id = 4, Name = "小赵", Age = 15, Score = 100 });

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

三:扩展方法用法


Linq有两种写法,查询表达式写法(from.....in.....)和扩展方法写法,两种方法都是相互兼容的,程序编译时会将查询表达式转换为扩展方法,只要实现了IEnumerable的接口就可以使用Linq的扩展方法


——Select:返回指定类型

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
var list = personList.Select(p => p.Name);
var list = personList.Select(p => new { id = p.Id, name = p.Name });

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

——Where:查询特定条件

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
var list = personList.Where((p) => p.Score >= 80 && p.Age < 50);

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

——OfType:查询特定数据类型 

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<object> objList = new List<object>()
{
"test1",
1,
1.34f,
"test2",
};

static void Main(string[] args)
{
var list = objList.OfType<string>();

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

——Join:将一个集合与另一个集合通过指定键合并,返回合并后的集合

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};
static List<Reward> rewardList = new List<Reward>()
{
new Reward(){ Id=1,Score=100,RewardName="奖励1"},
new Reward(){ Id=2,Score=80,RewardName="奖励2"},
new Reward(){ Id=3,Score=60,RewardName="奖励3"},
};

static void Main(string[] args)
{
var list = personList.Join(rewardList, p => p.Score, r => r.Score, (p, r) => new { pList = p, rList = r });

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

class Reward
{
public int Id;
public int Score;
public string RewardName;

public override string ToString()
{
return Id + "," + Score + "," + RewardName;
}
}

——GroupJoin: 将一个集合与另一个集合通过指定键分组

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};
static List<Reward> rewardList = new List<Reward>()
{
new Reward(){ Id=1,Score=101,RewardName="奖励1"},
new Reward(){ Id=2,Score=80,RewardName="奖励2"},
new Reward(){ Id=3,Score=60,RewardName="奖励3"},
};

static void Main(string[] args)
{
var list = personList.GroupJoin(rewardList, p => p.Score, r => r.Score, (p, result) => new { pList = p, count = result.Count() });

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

class Reward
{
public int Id;
public int Score;
public string RewardName;

public override string ToString()
{
return Id + "," + RewardName;
}
}

——OrderBy:对集合排序,默认是从小到大排序

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=60,Score=60},
new Person(){ Id=3,Name="小刘",Age=60,Score=80},
};

static void Main(string[] args)
{
var list = personList.OrderBy((p) => p.Age).ThenBy((p) => p.Score);

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

——Reverse:反转集合中元素的顺序

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
var list = personList.AsEnumerable().Reverse();

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}


——GroupBy:自身分组查询

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
new Person(){ Id=4,Name="小赵",Age=30,Score=60},
};

static void Main(string[] args)
{
var list = personList.GroupBy((p) => p.Score, (score, p) => new { score = score, count = p.Count() });

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

——Any和All:判断集合中是否满足某个/条件

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
bool b1 = personList.Any((p) => p.Age < 50);
bool b2 = personList.All((p) => p.Age < 50);

Console.WriteLine(b1);
Console.WriteLine(b2);
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

——Skip:跳过指定个元素查询

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
var list = personList.Skip(1);

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

——Take:只查询指定个元素

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
var list = personList.Take(2);

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

——Sum、Average、Max、Min:计算集合中指定数字类型数据的总和、平均值、最大值、最小值

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
var sum = personList.Sum((p) => p.Age);
var avg = personList.Average((p) => p.Age);
var max = personList.Max((p) => p.Age);
var min = personList.Min((p) => p.Age);

Console.WriteLine(sum);
Console.WriteLine(avg);
Console.WriteLine(max);
Console.WriteLine(min);
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

——Concat: 连接两个相同类型集合,合并为一个集合

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<int> numList1 = new List<int>() { 1, 2, 3 };
static List<int> numList2= new List<int>() { 4, 5, 6 };

static void Main(string[] args)
{
var list = numList1.Concat(numList2);

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

——Distinct:从集合中去除掉重复的元素

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<int> numList = new List<int>() { 1, 2, 3, 1, 2, 3 };

static void Main(string[] args)
{
var list = numList.Distinct();

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}


使用Distinct去重类中某个字段需要实现IEqualityComparer接口

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List<Person> personList = new List<Person>() { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var list = personList.Distinct(new Person()); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person : IEqualityComparer<Person> { public int Id; public string Name; public int Age; public int Score; public new bool Equals(Person x, Person y) { if (x == null || y == null) { return false; } if (x.Age == y.Age) { return true; } return false; } public int GetHashCode(Person obj) { return obj.Age.GetHashCode(); } public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } }

——ElementAt:得到集合中指定索引的元素,与[]作用相同

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<int> numList = new List<int>() { 1, 2, 3};

static void Main(string[] args)
{
var value = numList.ElementAt(2);

Console.WriteLine(value);
}
}

 ——Count:得到集合中满足指定条件的元素个数

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<int> numList1 = new List<int>() { 1, 2, 3 };

static void Main(string[] args)
{
var count = numList1.Count((n) => n >= 2);

Console.WriteLine(count);
}
}

——First/Single和Last:得到集合中第一个/最后一个元素(如果集合中包含多个元素,使用Single会报错)

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<int> numList1 = new List<int>() { 1, 2, 3 };

static void Main(string[] args)
{
var value1 = numList1.First((n) => n >= 2);
var value2 = numList1.Last((n) => n < 3);

Console.WriteLine(value1);
Console.WriteLine(value2);
}
}

——ToDictionary:将集合转换为字典 

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
var list = personList.ToDictionary(p => p.Id);

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}

——ToList: 将集合转换为list

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
static List<Person> personList = new List<Person>()
{
new Person(){ Id=1,Name="小明",Age=20,Score=100},
new Person(){ Id=2,Name="小王",Age=40,Score=80},
new Person(){ Id=3,Name="小刘",Age=60,Score=60},
};

static void Main(string[] args)
{
var dict = personList.ToDictionary(p => p.Id);
List<Person> list = dict.Values.ToList();

foreach (var temp in list)
{
Console.WriteLine(temp);
}
}
}

class Person
{
public int Id;
public string Name;
public int Age;
public int Score;

public override string ToString()
{
return Id + "," + Name + "," + Age + "," + Score;
}
}