![79a0e6363a36f5c1c146f9040409fece.png](https://i-blog.csdnimg.cn/blog_migrate/6f520eebd648c6788c27eff6a5d6cd34.jpeg)
第二十章 LINQ解析
20.1 LINQ概述
LINQ是语言集成查询的缩写(Language Integrated Query)。做软件开发肯定是离不开数据库的,里面有各种查询语句。LINQ的提出就是为了提供一种跨越各种数据源的统一查询方式-它主要包含4个组件-Linq to Objects,Linq to XML,Linq to DataSet和Linq to SQL。
Linq to SQL:可以查询基于关系数据库的数据。微软只实现了SQLServer的查询等操作。其他的第三方也实现了很多。
Linq to DataSet:查询DataSet中的数据,并能对数据进行增删等操作。
Linq to XML:该组件可以查询XML文件。
Linq to Objects:可以查询集合数据,如数组或List等。
20.2 LINQ查询表达式
查询表达式是以查询语法表示的查询。 查询表达式是一流的语言构造。 它如同任何其他表达式一样,可以在 C# 表达式有效的任何上下文中使用。 查询表达式由一组用类似于 SQL 或 XQuery 的声明性语法所编写的子句组成。 每个子句进而包含一个或多个 C# 表达式,而这些表达式可能本身是查询表达式或包含查询表达式。
查询表达式必须以 from 子句开头,且必须以 select 或 group 子句结尾。 在第一个 from
子句与最后一个 select
或 group
子句之间,可以包含以下这些可选子句中的一个或多个:where、orderby、join、let,甚至是其他 from 子句。 还可以使用 into 关键字,使 join
或 group
子句的结果可以充当相同查询表达式中的其他查询子句的源。
20.3 LINQ查询子句
- from 子句指定数据源以及范围变量
eg:
// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// //
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
using System.Collections.Generic;
using System.Linq;
namespace MSN
{
public class Student
{
public string LastName { get; set; }
public List<int> Scores { get; set; }
}
class MainClass
{
public static void Main(string[] args) {
List<Student> students = new List<Student>{
new Student
{
LastName="xiaogui",Scores=new List<int>{97,42,91,60}},
new Student
{
LastName="xiaozhan",Scores=new List<int>{50,92,81,60}},
new Student
{
LastName="xiaolan",Scores=new List<int>{32,32,81,90}},
new Student
{
LastName="xiaowan",Scores=new List<int>{92,22,81,60}},
};
var query = from stuent in students
from score in stuent.Scores
where score > 90
select new
{
Last = stuent.LastName,
score
};
//筛选出所有成绩大于90分的学生
foreach (var student in query)
{
Console.WriteLine("{0} Score:{1}", student.Last, student.score);
}
}
}
}
/*输出结果*/
/*
xiaogui Score:97
xiaogui Score:91
xiaozhan Score:92
xiaowan Score:92
*/
- where 子句 (筛选)制定查询条件,一个查询表达式可以包含多个where字句
eg:
// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// //
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
using System.Collections.Generic;
using System.Linq;
namespace MSN
{
class MainClass
{
public static void Main(string[] args) {
string[] str = { "a", "b", "c" };
var query = from s in str
where s == "a"
select s;
foreach (var s in query)
{
Console.WriteLine(s);
}
}
}
}
/*输出结果*/
/*
a
*/
- orderby 子句 (排序)
- 在查询表达式中,orderby字句可以使返回的序列(组)按升序或降序。
- 可以指定多个键,以便执行一个或多个次要排序操作
- 默认排序顺序为升序
- 编译时,orderby字句被转换为对OrderBy方法的调用。orderby字句中的多个键被转换为ThenBy方法调用
descending 降序
ascending 升序
eg:
// #region << 版 本 注 释 >>
// /*----------------------------------------------------------------
// // Copyright (C) 2019 极客部落
// // 版权所有。
// //
// // 文件名:Program.cs
// // 文件功能描述:
// //
// //
// // 创建者:GeekTribe
// // 时间:14:05
// //----------------------------------------------------------------*/
// #endregion
using System;
using System.Collections.Generic;
using System.Linq;
namespace MSN
{
class MainClass
{
public static void Main(string[] args) {
int[] int_array = { 123, 12, 345, 234, 75};
var query = from num in int_array
orderby num ascending //升序
select num;
foreach (int item in query) {
Console.WriteLine("item = {0}", item);
}
}
}
}
更多LINQ子句操作,可参见链接:https://docs.microsoft.com/zh-cn/dotnet/csharp/linq/