[转]LINQ会为我们带来什么?

来源:互联网 发布:什么是房地产网络销售 编辑:程序博客网 时间:2024/04/28 17:23
What LINQ will offer
LINQ会为我们带来什么?
.NET's new query operators.
.Net的新型查询操作
By David Rhys, Techworld
作者:David Phys TechWorld
Translated By Joey Yin CNBlogs.com
译者:Joey Yin Cnblogs.com
According to C#’s architect Anders Hejlsberg, there's an "impedance mismatch" between most programming languages and the database.
根据C#架构师Anders Hejlsberg的说法,在大多数编程语言和数据库之间,有一个“阻抗失谐”的问题。

What this means is that programming is all about procedural processing and how business (and other logic) works, while database access (at least in the relational database world) is all about declarations of what data you want (roughly, "SELECT A FROM B WHERE X=Y"), regardless of how you'll get it and what table it’s in. So, you issue simple declarative queries against a database; and when you get the data back into your program, in memory, you process it entirely differently. Wouldn't it be nice if there was a simple declarative language built into, say, C# or Visual Basic (Microsoft seems to think that these are the typical developer's primary programming languages, although no doubt some would disagree)?
阻抗失谐意味着数据库(至少对于关系型数据库)是对需要“什么样”的数据的一种定义(简单说来,就是SELECT A FROM B WHERE X=Y这种东东),而编程则全部是关于“怎样”通过程序处理业务和逻辑的工作,而不关心你怎样获取他,数据存储在哪个表里面。因此,我们依靠数据库来提供简单的查询接口,当你取出数据到你的应用中时,我们在内存中使用完全不同的方法来处理这些数据。所以,如果能够有一种简单的查询语言内嵌到我们的编程语言中,例如c#, vb(这是微软认为的多数开发者首选的编程语言,当然,很多反对者并不这么认为),那将是一个多么美好的世界!
This is what LINQ (Language INtegrated Query) will deliver, on the .NET platform. It's set of general-purpose declarative query operators that allow traversal, filter, and projection operations to be applied to any IEnumerable-based information source. LINQ queries read a bit like SQL but can be applied to non-relational datasources such as XML data and so on.
这就是LINQ(语言集成查询).NET平台上所要做的。他是一个多用途的查询操作集,允许在实现了Ienumerable接口的任何对象上进行数据的访问,过滤和发送操作。LINQ查询有一点像SQL语句,但是它可以应用于非关系型数据库的数据源中,例如XML等等。
The LINQ query language uses "lambda expressions", an idea originating in functional programming languages such as LISP - a lambda expression defines an unnamed function (and lambda expressions will also be a feature of the next C++ standard). In LINQ, lambdas are passed as arguments to its operators (such as Where, OrderBy, and Select) - you can also use named methods or anonymous methods similarly - and are fragments of code much like delegates, which act as filters.
LINQ查询使用Lambda表达式,这是一个源于Functional Programming语言(例如LISP)的新点子。Lambda表达式可以定义一个匿名的方法(Lambda表达式也会同样成为下一个版本的C++标准的一个新功能)。在LINQ查询中,Lambda表达式被当作参数传递给操作者(例如Where, OrderBySelect),当然你也可以使用实名方法或者匿名方法,当然这种使用更像是在使用代理了。
LINQ also defines a distinguished type, Expression (in the System.Expressions namespace), which means that you want to use an "expression tree" (an efficient in-memory data representation of a lambda expression) instead of a traditional method body - this may make the structure of the expression clearer.
LINQ定义了一个卓越的类型(distinguished type)(在System.Expressions 命名空间里),这意味着你需要使用一个“表达式树”(表达式树是一种更高效的表现形式,用以描述lambda表达式在内存中的数据)来取代传统的方法体,这将使得表达式的结构更加清晰。
Another idea LINQ borrows, this time from dynamic languages, is the idea of "extension methods". These allow third parties to add new methods to the public contract of a type without stopping individual type authors from providing specialised method implementations.
LINQ带来的另外一个概念“扩展方法”来源于动态语言。它允许第三方可以通过附加的方法扩展已存在的类型和构造类型。
One key feature of LINQ is its implicit support for SQL (DLinq) and XML (XLinq) integration, and this will assist full-blown object relational mapping products, as these can now take advantage of inbuilt language support. However, one possible issue is that it doesn't (as far as I can see) have the solid foundation in set theory that a query language based on a pukka relational algebra has (see Chris Date, “Database in Depth”, O’Reilly, ISBN 0-596-10012-4, for what this means). It may be possible for a programmer to confuse the quality of data sourced from a relational database (where integrity is enforced by the RDBMS – Relational DataBase Management System - itself and can largely be relied on) and data sourced from, say, an XML data store (where it isn't and can't be). In practice, since most RDBMSs (and the SQL language itself) compromise hugely with the purist relational model and therefore can't really claim the benefits from following it, this may not matter – LINQ is no worse than everything else (and relational algebra may be too hard for many people anyway). However, some people (myself included) think the loss of the formal rigor associated with the relational theory is to be regretted – although LINQ, in itself, doesn’t force any RDBMS to abandon relational theory, of course.
LINQ的一个主要的功能是它对SQLXML都提供强大的支持(分别通过DLingXLing)。这会极大的促进ORMapping技术的发展,因为现在可以利用内嵌语言了。但是,一个可能的情况是它在集合处理领域还缺乏像关系型数据库那样坚实的基础。这可能会使程序员被来源于关系型数据库(由关系型数据库自身保证其完整性)和其他数据源(例如XML,无法确立严格约束)的数据良莠不齐的品质搞糊涂。在实践中,许多关系型数据库(以及SQL语言本身)都没有非常严格地遵循关系型模型的理论,所以还不能简单声称这样做有多么重要。这是不公平的——LINQ不比其他这些差(关系代数对普通人来说,实在是太难了)。当然,尽管LINQ自己当然没有强迫任何关系型数据库放弃关系理论,但是很多人(包括我在内)都会为这种过分苛刻的联合感到遗憾。
In terms of the evolution of programming languages, LINQ represents a fusion of a typical OO language (C#) with ideas taken from modern dynamic languages such as Ruby and functional languages such as Lisp. It’s a thoroughly interesting idea and its availability probably will make C# a technical advance on Java, for a time at least. However, whether it actually delivers better (more powerful, more reliable and more maintainable) applications - which is presumably what it's all about at bottom for the companies employing .NET developers - depends on how C# and VB programmers take to such concepts as lambda expressions, expression trees and declarative programming.
从编程语言的演变来讲,LINQ代表的更多的是经典的面向对象语言(C#)和现代动态语言(Ruby)以及函数式语言(Lisp)的一种融合。它非常有趣而且可能会使C#在技术上暂时领先Java。无论如何,它最终能否帮助我们开发出更高质量(更强大,更可靠以及更可维护的)的应用——这些大概是所有公司在招聘.NET程序员时的最起码要求了——最终还是要取决于:我们的C#VB程序员是否喜欢这些概念:Lambda表达式,表达式树和宣告式编程。
原文地址:http://www.techworld.com/features/index.cfm?FeatureID=2982
转自:http://www.cnblogs.com/Joey/archive/2006/11/21/linq.html
原创粉丝点击