C#学习记录7——LINQ

LINQ(Language-INtegrated Query)语言集成查询,可以帮助我们将查询功能添加到C#中。可用于数据库等检索。

声明Query十分简单,语法上与SQL语言十分相似,只是有少许的区别

 

关于LINQ,在msdn上有101 Samples十分详细介绍解释LINQ如何使用在具体工程中。链接:https://code.msdn.microsoft.com/101-linq-samples-3fb9811b

 

一、Query Execution 查询的执行

介绍了Query执行中的特点

1.Query的延迟执行

 1             public void Linq99()
 2             {
 3 
 4                 // Queries are not executed until you enumerate over them.
 5                 int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 6 
 7                 int i = 0;
 8                 var simpleQuery =
 9                     from num in numbers
10                     select ++i;
11 
12                 // The local variable 'i' is not incremented
13                 // until the query is executed in the foreach loop.
14                 Console.WriteLine("The current value of i is {0}", i); //i is still zero
15 
16                 foreach (var item in simpleQuery)
17                 {
18                     Console.WriteLine("v = {0}, i = {1}", item, i);       
19                 }
20             }        

8-10行声明了一个qurey。由于没有where,所以numbers中所有元素都符合要求,每次找到符合的元素,++i

14行的输出语句,i仍旧是0。原因是声明query不会立刻执行

16行foreach语句才是真正执行query语句的位置

结果

v = 1, i = 1
v = 2, i = 2
v = 3, i = 3
v = 4, i = 4
v = 5, i = 5
v = 6, i = 6
v = 7, i = 7
v = 8, i = 8
v = 9, i = 9
v = 10, i = 10

2.一个query多次复用(Query Reuse)

 

 1             public void Linq101()
 2             {
 3 
 4                 // Deferred execution lets us define a query once
 5                 // and then reuse it later in various ways.
 6                 int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 7                 var lowNumbers =
 8                     from num in numbers
 9                     where num <= 3
10                     select num;
11 
12                 Console.WriteLine("First run numbers <= 3:");
13                 foreach (int n in lowNumbers)
14                 {
15                     Console.WriteLine(n);
16                 }
17 
18                 // Query the original query.
19                 var lowEvenNumbers =
20                     from num in lowNumbers
21                     where num % 2 == 0
22                     select num;
23 
24                 Console.WriteLine("Run lowEvenNumbers query:");
25                 foreach (int n in lowEvenNumbers)
26                 {
27                     Console.WriteLine(n);
28                 }
29 
30                 // Modify the source data.
31                 for (int i = 0; i < 10; i++)
32                 {
33                     numbers[i] = -numbers[i];
34                 }
35 
36                 // During this second run, the same query object,
37                 // lowNumbers, will be iterating over the new state
38                 // of numbers[], producing different results:
39                 Console.WriteLine("Second run numbers <= 3:");
40                 foreach (int n in lowNumbers)
41                 {
42                     Console.WriteLine(n);
43                 }
44                 Console.WriteLine("Second Run lowEvenNumbers query:");
45                 foreach (int n in lowEvenNumbers)
46                 {
47                     Console.WriteLine(n);
48                 }
49             }

首先注意到9,21行出现的where语句会根据一定条件选择结果
在13行和40行中两次使用的是同一个query语句。两次查询之间,将numbers中的所有数字全部取相反数。结果也随之改变。

可以说,创建query类似于创建了sql里面的一个view(视图),可以通过视图来查询制定条件的结果

结果

First run numbers <= 3:
1
3
2
0
Run lowEvenNumbers query:
2
0
Second run numbers <= 3:
-5
-4
-1
-3
-9
-8
-6
-7
-2
0
Second Run lowEvenNumbers query:
-4
-8
-6
-2
0

3.强制立即执行查询

 1             public void Linq100()
 2             {
 3 
 4                 // Methods like ToList(), Max(), and Count() cause the query to be
 5                 // executed immediately.            
 6                 int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 7 
 8                 int i = 0;
 9                 var immediateQuery = (
10                     from num in numbers
11                     select ++i)
12                     .ToList();
13 
14                 Console.WriteLine("The current value of i is {0}", i); //i has been incremented
15 
16                 foreach (var item in immediateQuery)
17                 {
18                     Console.WriteLine("v = {0}, i = {1}", item, i);
19                 }
20             }

与例1不同的在第12行,查询后使用了ToList()强制立刻执行当前的query查询。结合例2可以理解为,这里可以利于保存数据库的某个临时缓存结果,查看中期的结果。

结果

The current value of i is 10
v = 1, i = 10
v = 2, i = 10
v = 3, i = 10
v = 4, i = 10
v = 5, i = 10
v = 6, i = 10
v = 7, i = 10
v = 8, i = 10
v = 9, i = 10
v = 10, i = 10

二、Query排序

 1             public void Linq28()
 2             {
 3                 string[] words = { "cherry", "apple", "blueberry" };
 4 
 5                 var sortedWords =
 6                     from word in words
 7                     orderby word
 8                     select word;
 9 
10                 Console.WriteLine("The sorted list of words:");
11                 foreach (var w in sortedWords)
12                 {
13                     Console.WriteLine(w);
14                 }
15             }

7行orderby默认按照字符串的字母顺序从小到大排序
类似的还有:w.Length按照字符串长度从小到大排序

结果

The sorted list of words:
apple
blueberry
cherry

 

 

 

msdn101samples将其分成了几大部分,下载后的每个部分都有description文件,详细讲述了其中的例子,强烈推荐!

posted on 2015-04-25 13:49  DennisWong  阅读(170)  评论(0编辑  收藏  举报