0%

C Sharp 补课笔记 -- LINQ

语言集成查询

LINQ 数据源是支持泛型 IEnumerable<T> 接口或从中继承的接口的任意对象

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class LINQQueryExpressions
{
static void Main()
{
// Specify the data source.
int[] scores = new int[] { 97, 92, 81, 60 };
// Define the query expression.
IEnumerable<int> scoreQuery =
from score in scores
where score > 80
select score;// 此时scoreQuery并无数据
// Execute the query.执行foreach后才有数
foreach (int i in scoreQuery)
{
Console.Write(i + " ");
}
}
}
// Output: 97 92 81
1
2
3
4
5
6
7
8
9
10
11
12
var queryLondonCustomers3 =
from cust in customers
where cust.City == "London"
orderby cust.Name ascending//descending
select cust;

var query = from cust in Customer
where cust.City = "BeiJing"
select new {Name = cust.Name, City = cust.City};

// 等价于
var query = Customer.Where(c => c.City == "BeiJing").Select(n => new {Name = n.Name, City = n.Cith});

生成到xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Create the query.
var studentsToXML = new XElement("Root",
from student in students
let scores = string.Join(",", student.Scores)
select new XElement("student",
new XElement("First", student.First),
new XElement("Last", student.Last),
new XElement("Scores", scores)
) // end "student"
); // end "Root"
// Execute the query.
Console.WriteLine(studentsToXML);
/*
<Root>
<student>
<First>Svetlana</First>
<Last>Omelchenko</Last>
<Scores>97,92,81,60</Scores>
</student>
<student>
<First>Claire</First>
<Last>O'Donnell</Last>
<Scores>75,84,91,39</Scores>
</student>
<student>
<First>Sven</First>
<Last>Mortensen</Last>
<Scores>88,94,65,91</Scores>
</student>
</Root>
*/

Let标识符

引入查询表达式中任何表达式结果的标识符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var studentQuery5 =
from student in 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;
//select totalScore

var studentQuery6 =
from student in students
let totalScore = student.Scores[0] + student.Scores[1] +
student.Scores[2] + student.Scores[3]
select totalScore;
double averageScore = studentQuery6.Average();

var studentQuery8 =
from student in students
let x = student.Scores[0] + student.Scores[1] +
student.Scores[2] + student.Scores[3]
where x > averageScore
select new { id = student.ID, score = x };

// Join products and categories based on CategoryId
var query = from product in products
join category in categories on product.CategoryId equals category.Id
select new { product.Name, category.CategoryName };

比较集运算

Distinct

返回的序列包含输入序列的唯一元素

1
2
3
4
5
6
7
8
9
10
string[] planets = { "Mercury", "Venus", "Venus", "Earth", "Mars", "Earth" };
IEnumerable<string> query = from planet in planets.Distinct()
select planet;
/* This code produces the following output:
* *
Mercury
* Venus
* Earth
* Mars
*/

Except

回的序列只包含位于第一个输入序列但不位于第二个输入序列的元素

1
2
3
4
5
6
string[] planets1 = { "Mercury", "Venus", "Earth", "Jupiter" };
string[] planets2 = { "Mercury", "Earth", "Mars", "Jupiter" };
IEnumerable<string> query = from planet in planets1.Except(planets2)
select planet;

//Venus

Intersect

返回的序列包含两个输入序列共有的元素

1
2
3
4
5
6
7
8
9
10
string[] planets1 = { "Mercury", "Venus", "Earth", "Jupiter" };
string[] planets2 = { "Mercury", "Earth", "Mars", "Jupiter" };
IEnumerable<string> query = from planet in planets1.Intersect(planets2)
select planet;
/* This code produces the following output:
* *
Mercury
* Earth
* Jupiter
*/

Union

返回的序列包含两个输入序列的唯一元素

1
2
3
4
5
6
7
8
9
10
11
12
string[] planets1 = { "Mercury", "Venus", "Earth", "Jupiter" };
string[] planets2 = { "Mercury", "Earth", "Mars", "Jupiter" };
IEnumerable<string> query = from planet in planets1.Union(planets2)
select planet;
/* This code produces the following output:
*
*Mercury
* Venus
* Earth
* Jupiter
* Mars
*/