转:LINQ教程一:LINQ简介

原文地址:https://www.cnblogs.com/dotnet261010/p/8278793.html

一、为什么要使用LINQ

要理解为什么使用LINQ,先来看下面一个例子。假设有一个整数类型的数组,找到里面的偶数并进行降序排序。

在C#2.0以前,如果要实现这样的功能,我们必须使用'foreach'或'for'循环来遍历数组,先找到偶数然后在降序排序,相关代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LinqOfSelectOperation
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             // 查询出数组中的偶数并排序
14             int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };
15             // 定义一个整数类型的集合,用来存放数组中的偶数
16             List<int> list = new List<int>();
17             // 遍历数组查询出偶数放到集合中
18             foreach (int i in ints)
19             {
20                 // 如果是偶数,把偶数加入到集合中
21                 if (i % 2 == 0)
22                 {
23                     list.Add(i);
24                 }
25             }
26 
27             // 正序排序
28             list.Sort();
29             // 反转
30             list.Reverse();
31             // 输出
32             Console.WriteLine(string.Join(",",list));
33 
34             Console.ReadKey();
35         }
36     }
37 }


 2.使用for循环很麻烦,而且不可维护和可读。C#2.0引入了delegate,可以使用委托来处理这种场景,代码如下图所示:

1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LinqOfSelectOperation
 8 {
 9     // 定义委托
10     delegate bool FindEven(int item);
11 
12     class IntExtension
13     {
14         public static int[] where(int[] array, FindEven dele)
15         {
16             int[] result=new int[5];
17             int i = 0;
18             foreach (int item in array)
19             {
20                 if (dele(item))
21                 {
22                    result[i]=item;
23                     i++;
24                 }
25             }
26 
27             return result;
28         }
29     }
30     class Program
31     {
32         static void Main(string[] args)
33         {
34             // 查询出数组中的偶数并排序
35             int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };
36 
37             //delegate(int item){return item % 2 == 0;}表示委托的实现
38             List<int> list = IntExtension.where(ints, delegate(int item)
39             {
40                 return item % 2 == 0;
41             }).ToList();
42             // 正序排序
43             list.Sort();
44             // 反转
45             list.Reverse();
46             // 输出
47             Console.WriteLine(string.Join(",", list));
48 
49             Console.ReadKey();
50         }
51     }
52 }

所以,有了C#2.0,通过使用委托有了代理的优势,不必使用for循环来查询不同条件的数组。例如你可以使用相同的委托来查找数组中的奇数,并降序排序输出,代码如下图所示:

1using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LinqOfSelectOperation
 8 {
 9     // 定义委托
10     delegate bool FindEven(int item);
11 
12     class IntExtension
13     {
14         public static int[] where(int[] array, FindEven dele)
15         {
16             int[] result=new int[3];
17             int i = 0;
18             foreach (int item in array)
19             {
20                 if (dele(item))
21                 {
22                    result[i]=item;
23                     i++;
24                 }
25             }
26 
27             return result;
28         }
29     }
30     class Program
31     {
32         static void Main(string[] args)
33         {
34             // 查询出数组中的奇数并排序
35             int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };
36 
37             //delegate(int item){return item % 2 != 0;}表示委托的实现
38             List<int> list = IntExtension.where(ints, delegate(int item)
39             {
40                 return item % 2 != 0;
41             }).ToList();
42             // 正序排序
43             list.Sort();
44             // 反转
45             list.Reverse();
46             // 输出
47             Console.WriteLine(string.Join(",", list));
48 
49             Console.ReadKey();
50         }
51     }
52 }

虽然使用delegate可以使程序的可读性增加了,但是C#团队认为他们仍然需要使代码更加紧凑和可读,所以他们在C#3.0中引入了扩展方法、Lambda表达式、匿名类型等新特性,你可以使用C#3.0的这些新特性,这些新特性的使用LINQ的前提,可以用来查询不同类型的集合,并返回需要的结果。

下面的示例演示了如何使用LINQ和Lambda表达式根据特定条件来查询数组,示例代码如下:

1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LinqOfSelectOperation
 8 {  
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             // 查询出数组中的奇数并排序
14             int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };
15 
16             // 使用LINQ和Lambda表达式查询数组中的偶数
17             int[] intEvens= ints.Where(p => p % 2 == 0).ToArray();
18             // 使用LINQ和Lambda表达式查询数组中的奇数
19             int[] intOdds = ints.Where(p => p % 2 != 0).ToArray();
20 
21             // 输出
22             Console.WriteLine("偶数:" + string.Join(",", intEvens));
23             Console.WriteLine("奇数:" + string.Join(",", intOdds));
24 
25             Console.ReadKey();
26         }
27     }
28 }

在上面的例子中可以看到,我们在单个语句中使用LINQ和Lambda表达式指定不同的查询条件,因此,LINQ使代码更加紧凑和可读,并且它也可以用于查询不同的数据源。看到这里的时候,你可能会问:究竟什么是LINQ呢?下面将会具体讲解什么是LINQ。

二、什么是LINQ

长期以来,开发社区形成以下的格局:

1、面向对象与数据访问两个领域长期分裂,各自为政。

2、编程语言中的数据类型与数据库中的数据类型形成两套不同的体系,例如:

  C#中字符串用string数据类型表示。

  SQL中字符串用NVarchar/Varchar/Char数据类型表示。

3、SQL编码体验落后

  没有智能感知效果。

  没有严格意义上的强类型和类型检查。

4、SQL和XML都有各自的查询语言,而对象没有自己的查询语言。

上面描述的问题,都可以使用LINQ解决,那么究竟什么是LINQ呢?

LINQ(Language Integrated Query)即语言集成查询。

LINQ是一组语言特性和API,使得你可以使用统一的方式编写各种查询。用于保存和检索来自不同数据源的数据,从而消除了编程语言和数据库之间的不匹配,以及为不同类型的数据源提供单个查询接口。

LINQ总是使用对象,因此你可以使用相同的查询语法来查询和转换XML、对象集合、SQL数据库、ADO.NET数据集以及任何其他可用的LINQ提供程序格式的数据。

LINQ主要包含以下三部分:

1、LINQ to Objects      主要负责对象的查询。

2、LINQ to XML           主要负责XML的查询。

3、LINQ to ADO.NET   主要负责数据库的查询。

  LINQ to SQL

  LINQ to DataSet

  LINQ to Entities

三、LINQ的优势

1、熟悉的语言:开发人员不必为每种类型的数据源或数据格式学习新的语言。

2、更少的编码:相比较传统的方式,LINQ减少了要编写的代码量。

3、可读性强:LINQ增加了代码的可读性,因此其他开发人员可以很轻松地理解和维护。

4、标准化的查询方式:可以使用相同的LINQ语法查询多个数据源。

5、类型检查:程序会在编译的时候提供类型检查。

6、智能感知提示:LINQ为通用集合提供智能感知提示。

7、整形数据:LINQ可以检索不同形状的数据。

猜你喜欢

转载自www.cnblogs.com/wxxf/p/9235525.html