LINQ for Visual C# 2008

 

本文部分翻译了LINQ for Visual C# 2008,只是为了自己学习方便,并不追求“信雅达”,如果对大家的学习有帮助,可以转载,注不注出处无所谓。有错误请指正。

Chapter 1: LINQ to Objects


In this chapter we’ll study LINQ fundamentals by exploring its features for querying in memory objects. We’ll start with some simple examples to get an idea of what programming with LINQ to Objects involves, then we’ll look at examples for all of LINQ’s standard query operators.

在这一章,我们将通过探寻LINQ的memory objects查询特征来学习它的基本原理。我们将以一个简单的例子开始,来获得LINQ to Objects 的编程思想,然后我们将探究所有的LINQ的标准查询操作。
Introduction
Data domains are different from object domains. When we deal with objects like arrays and collections, we use iteration to retrieve their elements. If we’re looking for a particular element based on its content rather than its index, we have to use a loop and process each element individually. For example, for an array of strings there is no built-in method to retrieve all elements whose length is equal to a particular value.

 

 数据不同于对象。当我们处理诸如数组、集合之类的对象时,我们使用循环来检索他们的元素。如果我们查找一个基于它的内容而不是索引的特定的元素时,我们不得不使用一个loop循环来单独遍历每一个元素。例如,对于一个字符串数组,没有内建的方法来检索所有长度等于特定值的元素。


LINQ addresses this challenge by providing a uniform way to access data from any data source using familiar syntax. It lets us focus on working with data rather than on accessing it.LINQ to Objects can be used with any class that implements the IEnumerable<T> interface. Let’s look at how it works.

 

 LINQ通过提供一个统一的方法,使用我们熟知的语法来访问任何一种数据源的数据 应对了这个挑战。它使我们集中于使用使用数据而不是访问数据。LINQ to Objects 可以被用在任何继承IEnumerable<T>泛型接口的类中。让我们看看它是怎么工作的。
A Simple C# 3.0 LINQ to Objects Program
Listing 1-1 is a console program snippet that uses LINQ to Objects to
display a specific element in an array.

  1. Listing 1-1. Using LINQ to Objects with List<T>
  2. List<Person> people = new List<Person> {
  3. new Person() { ID = 1,
  4. IDRole = 1,
  5. LastName = "Anderson",
  6. FirstName = "Brad"},
  7. new Person() { ID = 2,
  8. IDRole = 2,
  9. LastName = "Gray",
  10. FirstName = "Tom"}
  11. };
  12. var query = from p in people
  13. where p.ID == 1
  14. select new { p.FirstName, p.LastName };
  15. ObjectDumper.Write(query);

In Listing 1-1 you define a collection of Person objects and insert a couple of elements. List<T> is a generic class that implements IEnumerable<T>, so it’s suitable for LINQ querying.


Next you declare a variable, query, to hold the result of a LINQ query.Don’t worry about the var keyword right now; it will be discussed later in this chapter, in “Implicitly Typed Local Variables.”

 

接着,你声明了一个变量query来放LINQ查询的结果。现在不要考虑var这个关键字,它将在本章后面的“Implicitly Typed Local Variables.(隐式声明局部变量)”中讨论。


You initialize query to a LINQ’s query expression. The from clause specifies a data source. The variable p represents an object in the people collection. The where clause specifies a condition for selecting from the
data source. You want to retrieve just the person whose ID equals 1, so youspecify a Boolean expression, p.ID == 1. Finally, the select clause specifies what Person data you’re interested in retrieving.

 

为你的LINQ表达式初始化查询。from子句定义了一个数据源。变量p代表了people集合中的一个对象。where子句定义了一个从数据源选择的条件。


The ObjectDumper class is a convenient utility for producing formatted output. It has only one method, Write(), which has three overloads. (Both the ObjectDumper.cs source code and the ObjectDumper.dll assembly come
with the book’s source code download.)

 

ObjectDumper类的作用是便于产生格式化输出。它只有一个方法:write()和三个重载。


This very simple example uses new features from C# 3.0. The first is a query expression that is similar to the one used in SQL and allows developers to use query language syntax that they are already accustomed
to. When the compiler finds a query expression in the code, it transforms that expression into C# method calls.

 

这是个用到了C#3.0新特性的非常简单的例子。首先是一个查询表达式,和SQL中用法十分相似,允许开发者使用早已熟悉的查询语言句法。当编译器在代码中发现一个查询表达式时,就会把这个表达式转换成C#的方法调用。

 

Listing 1-2. Transformed LINQ to Object Code

 

  1. var query = people
  2. .Where(p => p.ID == 1)
  3. .Select(p => new { p.FirstName, p.LastName } );

The from keyword has been removed, leaving just the collection, people,against which to perform the query. The where and select clauses are transformed into two method calls: Where<T>() and Select<T>(),
respectively. They have been concatenated so that the Where method’s result is filtered by the Select method.

 

from关键字被删除,只剩下people集合来执行查询。where 和select子句被分别转换成两个方法调用Where<T>() and Select<T>(),它们被连接起来,where方法的结果被select方法过滤掉了。

 

You may wonder how this is possible. C# 2.0 doesn’t provide these methods for the List<T> class or the new C# 3.0 version. C# 3.0 doesn’t add these new methods to every class in .NET Framework 3.5. The answer
is a new C# 3.0 feature called extension methods.

 

你可能会疑惑,怎么可能!C# 2.0 没有为List<T>类 提供这些方法 。C# 3.0没有在.NET Framework 3.5中添加这些方法到任何类中。答案是 C# 3.0 的新特性叫做扩展方法。

下一节   扩展方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值