LINQ - 入门教程

LINQ - 入门教程 首页 / LinQ入门教程 / LINQ - 入门教程

语言集成查询(英语:Language Integrated Query,缩写:LINQ),发音"link",是微软的一项技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,目前可支持C#以及Visual Basic .NET语言。2007年11月19日随.NET Framework 3.5发布了LINQ技术。

包括LINQ to Objects、LINQ to SQL、LINQ to Datasets、LINQ to Entities、LINQ to Data Source、LINQ to XML/XSD等。

由Visual Studio 2008引入并由Anders Hejlsberg设计的LINQ(语言集成查询)即使在不了解SQL,XML等查询语言的情况下也可以编写查询。LINQ查询可以针对各种数据类型编写。

LINQ查询示例

C#

无涯教程网

using System;
using System.Linq;

class Program {
   static void Main() {
   
      string[] words = {"hello", "wonderful", "LINQ", "beautiful", "Learnfk"};
		
      //Get only short words
      var shortWords = from word in words where word.Length <= 5 select word;
	    
      //Print each word out
      foreach (var word in shortWords) {
         Console.WriteLine(word);
      }	 
		
      Console.ReadLine();
   }
}

VB

Module Module1
   Sub Main()
      Dim words As String() = {"hello", "wonderful", "LINQ", "beautiful", "Learnfk"}
     
      ' Get only short words
      Dim shortWords=From word In words _ Where word.Length <= 5 _ Select word
     
      ' Print each word out.
	  
      For Each word In shortWords
         Console.WriteLine(word)
      Next
	  
      Console.ReadLine()
   End Sub
End Module	

编译并执行以上C#或VB的代码时,将产生以下输出-

hello 
LINQ 
Learnfk

LINQ语法

LINQ有两种语法。以下是这些。

Lambda语法

var longWords=words.Where( w  w.length > 10);
Dim longWords=words.Where(Function(w) w.length > 10)

Query语法

var longwords=from w in words where w.length > 10;
Dim longwords=from w in words where w.length > 10

LINQ体系结构

LINQ具有三层体系结构,其中最上层由语言扩展组成,而下层由数据源组成,这些数据源通常是实现IEnumerable <T>或IQueryable <T>通用接口的对象。架构如下所示。

LINQ Architecture

LINQ表达式

查询表达式不过是LINQ查询,它以类似于SQL的形式表示,并带有诸如Select,Where和OrderBy之类的查询运算符,表达式以关键字" From"开头。

要访问标准LINQ查询运算符,默认情况下应导入名称空间System.Query,这些表达式是使用C#3.0的声明性查询语法编写的。

下面的示例显示了完整的查询操作,其中包括数据源创建,查询表达式定义和查询执行。

C#

无涯教程网

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Operators {
   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;

         //Execute the query.
		 
         foreach (int i in scoreQuery) {
            Console.Write(i + " ");
         }
		 
         Console.ReadLine();
      }
   }
}

编译并执行上述代码后,将产生以下输出-

97 92 81

LINQ VS 存储过程

LINQ和存储过程之间存在一系列差异。这些差异在下面提到。

  • 存储过程遵循预期的执行计划,其速度比LINQ查询快得多。

  • 与存储过程相比,执行LINQ查询时更容易避免运行时错误,因为存储过程具有Visual Studio的Intellisense支持以及在编译时进行全类型检查。

  • LINQ允许通过使用.NET调试器进行调试,而对于存储过程则不是这样。

  • 与存储过程相比,LINQ提供了对多个数据库的支持,在存储过程中,必须为各种类型的数据库重新编写代码。

Need For LINQ

在LINQ之前,必须学习C#,SQL和将两者绑定在一起以形成完整应用程序的各种API,因为,这些数据源和编程语言面临着不匹配的问题;

以下是LINQ出现之前,开发人员在查询数据时使用了多少种不同技术的示例。

SqlConnection sqlConnection = new SqlConnection(connectString);
SqlConnection.Open();

System.Data.SqlClient.SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;

sqlCommand.CommandText = "Select * from Customer";
return sqlCommand.ExecuteReader (CommandBehavior.CloseConnection)  

有趣的是,在功能强大的代码行中,查询仅由后两个定义,使用LINQ,相同的数据查询也可以用一种可读的形式编写代码,就像下面提到的以下代码一样。

Northwind db = new Northwind(@"C:\Data\Northwnd.mdf");
var query = from c in db.Customers select c;

LinQ目录

LinQ目录

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

DevOps实战笔记 -〔石雪峰〕

现代C++编程实战 -〔吴咏炜〕

Java业务开发常见错误100例 -〔朱晔〕

爱上跑步 -〔钱亮〕

高楼的性能工程实战课 -〔高楼〕

爆款文案修炼手册 -〔乐剑峰〕

说透芯片 -〔邵巍〕

快手 · 音视频技术入门课 -〔刘歧〕

Rust 语言从入门到实战 -〔唐刚〕

好记忆不如烂笔头。留下您的足迹吧 :)