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

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

问题描述

什么是Linq的?我知道这是对数据库,但它有什么作用?

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

推荐答案

的LINQ代表语言集成查询

Linq stands for Language Integrated Query

而不是写YAQL(另一种查询语言)的,MS语言的开发者直接在他们的语言提供了一种前preSS查询(如C#和VB)。形成这些查询不依赖于事物的实现细节的技术被查询,这样就可以与几乎没有考虑的基本方式编写针对多个目标(数据库,内存中对象,XML)有效的查询,其中,查询将被执行。

Instead of writing YAQL (yet another query language), MS language developers provided a way to express queries directly in their languages (such as c# and vb). 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框架(3.5)。

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


  • Linq到对象 - 检查<一个href=\"http://msdn.microsoft.com/en-us/library/system.linq.enumerable_members.aspx\">System.Linq.Enumerable用于查询方法。这些目标的IEnumerable&LT; T&GT; ,允许任何类型的集合加入影片箱以类型安全的方式进行查询。这些查询依赖于编译.NET方法,而不是前pressions。

  • 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的任何东西 - 检查<一个href=\"http://msdn.microsoft.com/en-us/library/system.linq.queryable_members.aspx\">System.Linq.Queryable对于一些查询方法。这些目标的IQueryable&LT; T&GT; ,允许前pression树木可以通过底层的实现翻译的建设

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.

防爆pression树 - 检查<一个href=\"http://msdn.microsoft.com/en-us/library/system.linq.ex$p$pssions.aspx\">System.Linq.Ex$p$pssions命名空间。这是code作为数据。在实践中,你应该知道这方面的东西,但并不真的需要编写针对这些类型code。语言功能(如拉姆达前pressions)可以让您使用各种短手,以避免处理这些类型直接。

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#团队建立了一个数据访问技术。这只是工作。

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到实体 - 检查 System.Data.Objects 命名空间。特别要注意的的ObjectContext 。这是由ADO.Net团队建立了一个数据访问技术。它是复杂的,功能强大,难度比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到XML - 检查 System.Xml.Linq的命名空间。从本质上讲,人们不满足于的System.Xml 的东西。因此,MS重新写的,并采取了重写的优势,介绍一些方法,使人们更容易使用针对XML LinqToObjects。

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

一些不错的帮手类型,如 Func键并的Action 。这些类型与通用支持代表。飘宣告自己的自定义(和未互换)委托类型的日子。

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语言(vb.net,C#,铁蟒蛇,cobol.net ...)的.NET Framework的一部分,并且可用。

All of the above is part of the .Net Framework, and available from any .Net language (vb.net, c#, iron python, cobol.net ...).

好吧,到语言特性。我要坚持到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.

查询的COM prehension语法 - 这使您可以在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 redeclared for each clause.

的IEnumerable&LT;串GT;结果=
  从myCustomersÇ
  其中,c.Name.StartsWith(B)
  选择c.Name;


  • LAMBDA实施例pressions - 这是用于指定方法的简写。每一个C#编译器将转化为要么是匿名方法或真正的 System.Linq.Ex pressions.Ex pression 。你真的需要了解这些使用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&LT;串GT;结果= myCustomers
  。凡(C =&GT; c.Name.StartsWith(B))
  。选择(C =&GT; c.Name);


  • 匿名类型 - 有时,编译器有足够的信息来为您创建一个类型。这些类型是不是真正的匿名...编译器的名字时,他们就使他们。但是,这些名称在编译时,这是为时已晚,开发人员使用这个名字在设计时进行。

myCustomers.Select(C =&gt;新建
{
  NAME = c.Name;
  年龄= c.Age;
})


  • 隐式类型 - 有时,编译器从初始化它可以断定类型为你足够的信息。您可以指示编译器使用var关键字来这样做。隐式类型需要为匿名类型声明变量,因为程序员可能无法使用匿名类型的名称。

//编译器将确定名称是一个IEnumerable&LT;串GT;

VAR名= myCustomers.Select(C =&GT; c.Name);

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

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