C# 中的LINQ
Linq 简介
语言集成查询(LINQ)是将一系列的功能,集成到C#语言中的技术的统称
Linq 可以应用的场景:
数据库
XML 文档
ADO.net 数据集
IEnumerable 接口的任何对象集合
第三方的 web 服务
- 在写完Linq 语句以后,其本身只是存储查询变量。并不会真正的执行。只有当调用 ToList 和 ToArray 方法的时候,才会真正的执行,而且调试的话,也进不去 Linq 语句。
- 一般使用 Tolist 这种写法,我个人也没调试出来着两种方法有什么区别。
查询表达式可以由三个部分组成:
- 获取数据源
- 建立查询表达式
- 执行查询
下边的例子
static void Main()
{
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
var numQuery =
from num in numbers //建立结果集
where (num % 2) == 0 //查询条件
select num; //选出来
// 3. Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
}
带有-排序-分组的语句
var queryLondonCustomers3 =
from cust in customers
where cust.City == "London"
orderby cust.Name ascending
//orderby cust.Name descending
select cust;
综合案例:
1.建立一个 student 类
public class Student
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public List<int> Scores;
}
// Create a data source by using a collection initializer.
static List<Student> students = new List<Student>
{
new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},
new Student {First="Claire", Last="O'Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},
new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}},
new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}},
new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}},
new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},
new Student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}},
new Student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}},
new Student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}},
new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}},
new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91}}
};
2.把第一科成绩 > 90 ,第四门成绩 < 90 的人筛选出来,并且按照 升序 排序
public static void show()
{
var studentsss =
from stu in Student.students
where stu.Scores[0] > 90 && stu.Scores[3]<90
orderby stu.Scores[0] ascending
// orderby stu.Scores[0] descending
select stu;
foreach (var item in studentsss)
{
Console.Write($"***{item.Last}****{item.First}****");
Console.Write("每个人的成绩是");
foreach (var items in item.Scores)
{
Console.Write(items.ToString()+"--");
}
Console.WriteLine();
}
}
}
结果如下:
3.结果进行分组
对结果进行分组的时候,注意分组后的结果,想要读出来,必须使用两个 foreach 循环
public static void show()
{
//怎么分组查找
var studentQuery2 =
from studentaa in Student.students
group studentaa by studentaa.Last[0];
// 怎么把分组的结果打印出来
foreach (var studentGroup in studentQuery2)
{
Console.WriteLine(studentGroup.Key);
foreach (Student student in studentGroup)
{
Console.WriteLine($"{student.First}**{student.Last}");
}
} }
最后输出的结果:
Let 关键字
Let 关键字可以用来储存结果
代码如下:
public static void Show()
{
var studentQuery5 =
from student in Student.students
let totalScore = student.Scores[0] + student.Scores[1] +
student.Scores[2] + student.Scores[3]
where totalScore / 4 < student.Scores[0]
select student.Last + " " + student.First;
foreach (string s in studentQuery5)
{
Console.WriteLine(s);
}
}
运行结果如下: