LINQ实践教程

Posted RC7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LINQ实践教程相关的知识,希望对你有一定的参考价值。

本教程旨在快速掌握LINQ基础用法,以及在此基础上构建复杂LINQ查询,没有任何啰嗦,直接上手。

示例1:最简单的LINQ查询

int[] numbers = { 5, 1, 8, 6, 2, 6, 4, 9, 10, 7, 3 };
var nums1 = from n in numbers where n < 5 select n;
foreach (var x in nums1)
{
Console.Write("{0}, ", x);
}
输出结果:
1, 2, 4, 3,


示例2:使用LINQ函数查询

int[] numbers = { 5, 1, 8, 6, 2, 6, 4, 9, 10, 7, 3 };
var nums1 = numbers.Where(n => n < 5);
foreach (var x in nums1)
{
Console.Write("{0}, ", x);
}
输出结果:

1, 2, 4, 3,


示例3:使用join子句

join类似于SQL中的join

Student[] students =
{
new Student {Id = 1, Name = "Zhangsan"},
new Student {Id = 2, Name = "Lisi"},
new Student {Id = 3, Name = "Wangwu"}
};
StudentCourse[] studentCourses =
{
new StudentCourse {Id = 1, CourseName = "Math"},
new StudentCourse {Id = 1, CourseName = "Chinese"},
new StudentCourse {Id = 2, CourseName = "Math"},
new StudentCourse {Id = 2, CourseName = "English"},
new StudentCourse {Id = 3, CourseName = "Math"}
};

var sc = from s in students
join c in studentCourses on s.Id equals c.Id
where c.Id == 1
select new {s, c};
foreach (var student in sc)
{
Console.WriteLine("Id={0}, Name={1}, Course={2}", student.s.Id, student.s.Name, student.c.CourseName);
}
输出结果:

Id=1, Name=Zhangsan, Course=Math
Id=1, Name=Zhangsan, Course=Chinese


示例4:使用let子句

let子句接受一个表达式的运算并把它赋值给一个需要在其他运算中使用的标识符。

var group1 = new int[] {3, 4, 5, 6};
var group2 = new int[] {6, 7, 8, 9};
//找出能够满足两数相加为12的所有数据
var someInts = from a in group1
from b in group2
let sum = a + b
where sum == 12
select new {a, b, sum};
foreach (var x in someInts)
{
Console.WriteLine(x);
}
输出结果:

{ a = 3, b = 9, sum = 12 }
{ a = 4, b = 8, sum = 12 }
{ a = 5, b = 7, sum = 12 }
{ a = 6, b = 6, sum = 12 }


示例5:使用orderby子句

注意,与SQL不同,order和by之间没有空格,默认排序是升序,可以使用ascending和descending关键字设定排序方式

int[] numbers = { 5, 1, 8, 6, 2, 6, 4, 9, 10, 7, 3 };
var results = from n in numbers where n < 5 orderby n descending select n;
foreach (var result in results)
{
Console.WriteLine(result);
}
输出结果:

4
3
2
1


示例6:使用group...by子句

StudentCourse[] studentCourses =
{
new StudentCourse {Id = 1, CourseName = "Math"},
new StudentCourse {Id = 1, CourseName = "Chinese"},
new StudentCourse {Id = 2, CourseName = "Math"},
new StudentCourse {Id = 2, CourseName = "English"},
new StudentCourse {Id = 3, CourseName = "Math"}
};
var query = from sc in studentCourses
group sc by sc.CourseName;
foreach (var sc in query)
{
Console.WriteLine("Current Group Key={0}",sc.Key);//sc.Key是当前分组的Key值
//注意,这里需要再次迭代才能获取实际数据的值
foreach (var s in sc)
{
Console.WriteLine(" Id={0}, CourseName={1}", s.Id, s.CourseName);
}
}
输出结果:

Current Group Key=Math
Id=1, CourseName=Math
Id=2, CourseName=Math
Id=3, CourseName=Math
Current Group Key=Chinese
Id=1, CourseName=Chinese
Current Group Key=English
Id=2, CourseName=English

 

 

示例7:查询延续:使用into子句

var group1 = new int[] { 3, 4, 5, 6 };
var group2 = new int[] { 4, 5, 6, 7 };
var query = from a in group1
join b in group2 on a equals b
into group12//查询延续
from c in group12
select c;
foreach (var item in query)
{
Console.WriteLine(item);
}
输出结果:

4
5
6

 

示例8:标准查询运算符

标准查询运算符由一系列API组成,它能让我们查询任何。NET数组或集合。标准查询运算符的主要特征如下:

被查询的集合对象叫做序列,它必须实现IEnumerable<T>接口
标准查询运算符使用函数语法
一些运算符返回Ienumerable对象(或其他序列),而其他也些运算符返回标量。返回标量的运算符立即执行,并返回一个值,而不是可枚举类型对象。
很多操作都以一个谓词作为参数,谓词是一个方法,它以对象作为参数,根据对象是否满足某个条件而返回true或false
如下代码演示了Sum和Count的使用:

var numbers = new int[] {2, 3, 5};
int total = numbers.Sum();
int count = numbers.Count();
Console.WriteLine("Total={0}, Count={1}", total, count);
输出结果:

Total=10, Count=3

 

以下是全部标准查询运算符,大家可以点进去查看具体使用方法:

 

以上是关于LINQ实践教程的主要内容,如果未能解决你的问题,请参考以下文章

30分钟LINQ教程

LINQ教程

Linq教程

30分钟LINQ教程

30分钟LINQ教程

C#图解教程 第十九章 LINQ