什么是 LINQ,它有什么作用? [英] What is LINQ and what does it do?

查看:21
本文介绍了什么是 LINQ,它有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是LINQ?我知道它是用于数据库的,但它有什么作用?

What is LINQ? I know it's for databases, but what does it do?

推荐答案

LINQ 代表语言集成查询.

Microsoft 语言开发人员提供了一种直接用他们的语言(例如 C# 和 Visual Basic)表达查询的方法,而不是编写 YAQL(另一种查询语言).形成这些查询的技术不依赖于被查询事物的实现细节,因此您可以针对许多目标(数据库、内存中对象、XML)编写有效的查询,而实际上无需考虑查询将被执行.

Instead of writing YAQL (Yet Another Query Language), Microsoft language developers provided a way to express queries directly in their languages (such as C# and Visual Basic). The techniques for forming these queries do not rely on the implementation details of the thing being queried, so that you can write valid queries against many targets (databases, in-memory objects, XML) with practically no consideration of the underlying way in which the query will be executed.

让我们从属于 .NET Framework (3.5) 的部分开始探索.

Let's start this exploration with the parts belonging to the .NET Framework (3.5).

  • LINQ To Objects - 检查 System.Linq.用于查询方法的可枚举.这些目标 IEnumerable,允许以类型安全的方式查询任何类型化的可循环集合.这些查询依赖于编译的 .NET 方法,而不是表达式.

  • LINQ To Objects - examine System.Linq.Enumerable for query methods. These target IEnumerable<T>, allowing any typed loopable collection to be queried in a type-safe manner. These queries rely on compiled .NET methods, not Expressions.

LINQ To Anything - 检查 System.Linq.对于某些查询方法可查询.这些目标IQueryable,允许构建可由底层实现翻译的表达式树.

LINQ To Anything - examine System.Linq.Queryable for some query methods. These target IQueryable<T>, allowing the construction of Expression Trees that can be translated by the underlying implementation.

表达式树 - 检查 System.Linq.Expressions 命名空间.这是作为数据的代码.在实践中,您应该了解这些内容,但实际上并不需要针对这些类型编写代码.语言特性(例如 lambda 表达式)可以让您使用各种简写来避免直接处理这些类型.

Expression Trees - examine System.Linq.Expressions namespace. This is code as data. In practice, you should be aware of this stuff, but don't really need to write code against these types. Language features (such as lambda expressions) can allow you to use various short-hands to avoid dealing with these types directly.

LINQ To SQL - 检查 System.Data.Linq 命名空间.特别注意DataContext.这是一种由 C# 团队构建的 DataAccess 技术.它只是有效.

LINQ To SQL - examine the System.Data.Linq namespace. Especially note the DataContext. This is a DataAccess technology built by the C# team. It just works.

LINQ To Entities - 检查 System.Data.Objects 命名空间.特别注意ObjectContext.这是 ADO.NET 团队构建的 DataAccess 技术.它比 LINQ To SQL 复杂、强大且更难使用.

LINQ To Entities - examine the System.Data.Objects namespace. Especially note the ObjectContext. This is a DataAccess technology built by the ADO.NET team. It is complex, powerful, and harder to use than LINQ To SQL.

LINQ To XML - 检查 System.Xml.Linq 命名空间.本质上,人们对System.Xml 中的内容并不满意.所以微软重新编写了它,并利用重新编写的优势引入了一些方法,可以更容易地针对 XML 使用 LINQ To Objects.

LINQ To XML - examine the System.Xml.Linq namespace. Essentially, people weren't satisfied with the stuff in System.Xml. So Microsoft re-wrote it and took advantage of the re-write to introduce some methods that make it easier to use LINQ To Objects against XML.

一些不错的帮助器类型,例如 Func操作.这些类型是具有通用支持的委托.声明自己的自定义(不可互换)委托类型的日子已经一去不复返了.

Some nice helper types, such as Func and Action. These types are delegates with Generic Support. Gone are the days of declaring your own custom (and un-interchangable) delegate types.

以上所有内容都是 .NET Framework 的一部分,可用于任何 .NET 语言(VB.NET、C#、IronPython、COBOL .NET 等).

All of the above is part of the .NET Framework, and available from any .NET language (VB.NET, C#, IronPython, COBOL .NET etc).

好的,语言功能.我将坚持使用 C#,因为这是我最了解的.VB.NET 也有几个类似的改进(还有一些 C# 没有得到 - XML 文字).这是一个简短且不完整的列表.

Ok, on to language features. I'm going to stick to C#, since that's what I know best. VB.NET also had several similar improvements (and a couple that C# didn't get - XML literals). This is a short and incomplete list.

  • 扩展方法 - 这允许您添加"要键入的方法.该方法实际上是传递类型实例的静态方法,并且仅限于类型的公共契约,但是对于向您不控制的类型(字符串)或添加(完全实现)的类型添加方法非常有用) 接口的辅助方法.

  • Extension Methods - this allows you to "add" a method to type. The method is really a static method that is passed an instance of the type, and is restricted to the public contract of the type, but it very useful for adding methods to types you don't control (string), or adding (fully implemented) helper methods to interfaces.

查询理解语法 - 这允许您编写类似 SQL 的结构.所有这些内容都被转换为 System.Linq.Queryable 或 System.Linq.Enumerable 上的方法(取决于 myCustomers 的类型).它是完全可选的,没有它您也可以很好地使用 LINQ.这种查询声明风格的一个优点是范围变量是有作用域的:不需要为每个子句重新声明它们.

Query Comprehension Syntax - this allows you to write in a SQL Like structure. All of this stuff gets translated to the methods on System.Linq.Queryable or System.Linq.Enumerable (depending on the Type of myCustomers). It is completely optional and you can use LINQ well without it. One advantage to this style of query declaration is that the range variables are scoped: they do not need to be re-declared for each clause.

IEnumerable<string> result =
 from c in myCustomers
 where c.Name.StartsWith("B")
 select c.Name;

  • Lambda 表达式 - 这是指定方法的简写.C# 编译器会将每个转换为匿名方法或真正的 System.Linq.Expressions.Expression.你真的需要了解这些才能很好地使用 Linq.由三部分组成:参数列表、箭头和方法体.

  • Lambda Expressions - This is a shorthand for specifying a method. The C# compiler will translate each into either an anonymous method or a true System.Linq.Expressions.Expression. You really need to understand these to use Linq well. There are three parts: a parameter list, an arrow, and a method body.

    IEnumerable<string> result = myCustomers
     .Where(c => c.Name.StartsWith("B"))
     .Select(c => c.Name);`
    

  • 匿名类型 - 有时编译器有足够的信息为您创建类型.这些类型并不是真正匿名的:编译器在生成它们时命名它们.但这些名称是在编译时生成的,开发人员在设计时使用该名称为时已晚.

  • Anonymous Types - Sometimes the compiler has enough information to create a type for you. These types aren't truly anonymous: the compiler names them when it makes them. But those names are made at compile time, which is too late for a developer to use that name at design time.

    myCustomers.Select(c => new 
    {
      Name = c.Name;
      Age = c.Age;
    })
    

  • 隐式类型 - 有时编译器从初始化中获得足够的信息,可以为您找出类型.您可以使用 var 关键字指示编译器执行此操作.隐式类型需要为匿名类型声明变量,因为程序员不能使用匿名类型的名称.

    // The compiler will determine that names is an IEnumerable<string>
    var names = myCustomers.Select(c => c.Name);
    

  • 这篇关于什么是 LINQ,它有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆