2011年7月30日 星期六

LINQ Unleashed: for C#

Bear在這裡買的
amazon的參考

LINQ編程技術內幕/Microsoft核心技術叢書
作者:(美)基默|譯者:唐學韜
出版社:機械工業
ISBN:9787111267591
出版日期:2009/06/01
頁數:414
人民幣:RMB 59 元




P.19(原文P.26)
想要快速弄出一個事件處理的方法,只需輸入 object.eventname 以及 += 運算符,然後按兩下 Tab 鍵即可。

To quickly stub out an event-handling method, type the object.eventname, the += operator, and press the Tab key twice.

P.28(原文P.36)
初始化器 {FirstName=p.Name} 的意思是,將結果集初始化成一個含有屬性 FirstName 的匿名類型。

Listing 2.5 defines a Person class. The statement beginning with var names uses a LINQ query to select all of the Name values. (Don’t worry about the query too much right now.) The initializer {FirstName=p.Name} means to initialize the resultset as an anonymous type with a property named FirstName. The foreach statement shows how we now refer to the Name values through the projected FirstName property.

P.29(原文P.38)
參考一下this可以這樣用。

public PointF[] GetPoints()
{
return (from p in this select p.MyPoint).ToArray();
}
public ColoredPointList Clone()
{
ColoredPointList list = new ColoredPointList();
list.AddRange((from o in this select o.Clone()));
return list;
}

P.41(原文P.51)
轉換運算符 ToArray, OfType, Cast, AsEnumerable, ToList, ToDictionary, 及 ToLookup。

Prevalent, tedious, and time-consuming work cries out for convenience tools. The new version of .NET has answered this cry. The following sections explore the conversion operators ToArray, OfType, Cast, AsEnumerable, ToList, ToDictionary, and ToLookup.

P.44(原文P.54)
轉換運算符 OfType 返回的是一個 IEnumerable 集合,該集合只含有由參數 T 定義的類型。

The conversion operator OfType returns an IEnumerable collection of only the types defined by the parameter T. For example, initializing a list of objects where some are integers, you can quickly extract just the integers, as demonstrated in Listing 2.13. Listing 2.13 returns an IEnumerable with 1 and 4 in the resultset.

要想在元素不能從源類型轉換為目標類型時收到一個異常的話,可以使用  Cast 轉換運算符。
(Bear: 失敗時會拋出 InvalidCastException)

To receive an exception if elements of the source type cannot be converted to the target type, use the Cast conversion operator. If you use numbers from Listing 2.13, you’ll receive an InvalidCastException when the Cast operator hits the string “two” (see Listing 2.14).

P.45(原文P.55)
許多類型都實現了 IEnumerable 。其中一部份還實現了與 IEnumerable 一模一樣的公共方法。比如說,如果你有一個實現了 Where 方法的類型(跟 IEnumerable 一樣),那麼在 MyList 上調用 Where 將使用 MyList 的 Where 實現。如果先調用 AsEnumerable 方法然後再調用 Where 的話,你調用的就是 IEnumerable 的 Where 而不是 MyList 的。
(Bear:這裡一定要看它的範例才會了解作者的意思)

Many types implement IEnumerable. Some of these types implement public members that are identical to IEnumerable. For instance, if you have a type MyList that implements a Where method (as does IEnumerable), invoking Where on MyList would call MyList’s implementation of Where. By calling the AsEnumerable method first and then calling Where, you invoke IEnumerable’s Where method instead of MyList’s (see Listing 2.15).

P.46(原文P.56)
第一個 Where 調用用到的是 MyList 的 Where。調用了 AsEnumerable() 之後接下來的調用就是 List 的 Where 了(這是 List的 IEnumerable實現的一部份)。

In Listing 2.15, the first Where call invokes MyList’s Where. After the call to AsEnumerable(), the next call is to List’s Where as part of List’s implementation of IEnumerable.

將查詢結果轉換為 List 使我們能夠向結果集添加額外的元素

LISTING 2.16 Converting Query Results to a List Lets Us Add Additional Elements to the Resultset

P.47(原文P.58)
Lookup 類似於 Dictionary ,不過, Dictionary 每個鍵只對應一個值,而 Lookup 則將鍵與一組值映射起來。Lookup沒有公共構造函數,而且是不可變的。在創造 Lookup 之後,不能添加或刪除其中的元素或鍵。

ToLookup converts an IEnumerable to a Lookup type. Lookup is like a dictionary, but where a Dictionary uses a single key value, Lookup maps keys to a collection of values. Lookups have no public constructor and are immutable. You cannot add or remove elements or keys after they are created.

P.50(原文P.61)
從概念上講擴展方法就是裝飾模式(Decorator,一種結構型模式)的一種實現。

Conceptually, think of extension methods as an implementation of the Decorator Structural pattern. If you check http://www.dofactory.com, you will see that the actual decorator is intended to add behavior dynamically using inheritance and composition. The ToolTip control is a closer technical match to the decorator, but extension methods are logically close enough.

P.52(原文P.63)
擴展方法是靜態類中的靜態方法。

Extension methods are static methods in static classes (see Listing 3.1).

擴展方法是通過實例語法來調用的。

Extension methods are invoked using instance syntax.

P.52(原文P.64)
擴展方法遵循一個固定的模式。定義一個公共靜態類,然後在這個類裡面定義一個公共靜態方法。該方法的第一個參數必需使用 this 修飾符。第一個參數(用 this 修飾的)代指將要被擴展的類。在第一個參數之後,加上其他參數。這些靜態方法既可以通過參數來重載,也可以是泛型方法。

Extension methods follow a consistent pattern. Define a public static class. In that class, define a public static method. The first argument of the method must use the this modifier. The first argument, modified by this, represents the class of the object being extended. After the first argument, add additional arguments. Static methods can be overloaded by parameters and can be generic methods. This section explores extension methods, overloaded extension methods, and generic extension methods.

P.61(原文P.74)
簡而言之,這意味著LINQ的基礎就是擴展方法(還有泛型)。因此,在你編寫了一個LINQ語句之後,編譯器就會據此發出一系列外型更加複雜的泛型方法。

In short, this means that LINQ’s underpinnings are based on extension methods (and generics), so that when you write a LINQ statement, the compiler is actually emitting the call to the more complex-looking generic methods. Listing 3.6 contains a simple LINQ query that returns even numbers from an array. Listing 3.7 shows the Microsoft Intermediate Language (MSIL or IL)—which was retrieved using Intermediate Language Disassembler (ILDASM)—emitted that contains the more verbose calls to the extension methods in System.Linq.

P.66(原文P.80)
分部方法就是為自動生成的代碼而準備的占位符。

Bill said, “Partial methods are placeholders for generated code.”

而分部方法則能夠讓生產者只修改標記為 partial 的方法。如果消費者想要在某處插入一些行為,則需要提供該分部方法的一個實現。如果沒有給出實現,則該分部方法將會被忽略。

The problem with code-generated code has typically been that if the consumer changed the generated code and regenerated the code, the user’s changes were overwritten. One solution to this problem was to inherit from generated code and write custom changes in the child class. However, partial methods let the producer stub out the method signature with a partial method. If the consumer wants to insert some behavior at that point, the consumer provides an implementation for the partial method. If no implementation is provided, the partial method is stripped out. This is clever and useful for code generators. (The producer and consumer can be the same programmer, of course.)

分部方法是私有的,不過不能在字面上使用訪問修飾符;也就是說,你不能顯式使用 private 關鍵字。

Partial methods are private, but no literal access modifiers are permitted; that is, you can’t use the private keyword explicitly.

P.82(原文P.101)
不過,.NET 3.5定義了諸如 Action and Func 這樣的匿名委托,它們可以用來代替 delegate 語句。這就意味著,你不用再去定義 delegate 語句了。

A Lambda Expression is a concise delegate. The left side (of the =>) represents the arguments to the function and the right side is the function body. In Listing 5.4, the Lambda Expression is assigned to the delegate FunctionPointer, but .NET 3.5 defines anonymous delegates like Action and Func, which can be used instead of the more formal, longer delegate statement. This means that you don’t have to define the delegate statement (as shown in Listing 5.4). Listing 5.5 shows the revision of Listing 5.4, replacing the delegate statement with the generic Action delegate type.

P.90(原文P.109)
Lambda表達式其實就是非常簡短的函數,也就是內聯函數。因此,只要能使用函數和匿名委托的地方,你都可以直接使用Lambda表達式。

It is worth remembering that Lambda Expressions are just very short functions, basically inline functions. Therefore, you can assign a Lambda Expression anywhere you would use a function or anonymous delegate. Remember, as is shown in Listing 5.13, the left side of the <= operator is the inputs and the right side represents the method body. Therefore, to match a typical EventHandler, you need the left side to have an object and EventArgs inputs and the right side to do the typical kinds of things you would do with more.

P.103(原文P.124)
示例6.2將返回值1和3(如圖6-2所示)。雖然可以用convert類及其ToInt32方法把7M(一個decimal數)轉換成一個整型值,但是OfType卻不會執行這個操作。

The code in Listing 6.2 returns the values 1 and 3 (see Figure 6.2). Although you can convert 7M (a decimal number) to an integer with the Convert class and ToInt32 method, OfType does not perform that conversion. You can learn more about conversion operators in Chapter 2.

P.106(原文P.128)
Bear: 這樣的註解方式看起來還不錯用,很清楚。但更好的作法應該用變數或常數(const, readonly)取代 magic number

var sequence = Enumerable.Range(10/*start*/, 100/*count*/);

P.109(原文P.131)
所有元素運算都是實現擴展方法的,且沒有相應的LINQ關鍵字。元素方法有: ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, 以及 SingleOrDefault。他們均用於從序列中返回單個元素。

The element operations are all implemented as extension methods without LINQ keywords. The element methods are: ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, and SingleOrDefault. Each in its way is designed to return a single element from a sequence.

P.113(原文P.138)
在一個集合上調用Revise擴展方法即可翻轉該集合中的元素。 Revise 在C#中並沒有相應的LINQ關鍵字。

A collection of items can also be reversed by invoking the Reverse extension method on a collection. Reverse is not a supported LINQ keyword in C#.

如果查閱 Microsoft Developer Network (MSDN) 線上幫助的話,它會告訴你那些擴展方法有對應的LINQ關鍵字。比如,排序可以由 OrderBy 擴展方法或orderby關鍵字來實現。而Revise 在C#中就只能調用擴展方法。

If you look at the Microsoft Developer Network (MSDN) help, it indicates which extension methods have LINQ equivalents, for example, sorting is handled by the OrderBy extension method and the orderby keyword, but Reverse is only supported in C# by calling the extension method.

P.115(原文P.139)
如果你的答案是 IEnumerable ,那麼就說明你真的是明白了。準確的類型是 IOrderedEnumerable

In Listing 7.1, the query is assigned to the anonymous type sorted, and the sort behavior is introduced by the orderby word clause. Can you guess the compiler-generated type of the variable sorted? If you answered IEnumerable, you are on top of things. The actual type is IOrderedEnumerable, but you wouldn’t know that without detailed exploration of the .NET Framework and the emitted Microsoft Intermediate Language (MSIL). Listing 7.2 shows the introduction of the descending modifier on the orderby clause.

P.120(原文P.145)
值 StringSplitOptions.RemoveEmptyEntries 用於移除結果中的空白字符串或空字符串。

In Listing 7.6, String.Split is used to split the quote by spaces, periods, and commas. The StringSplitOptions.RemoveEmptyEntries value removes whitespace or empty entries, such as would appear after the comma followed by a space (“...will to win, few have the will”) in the middle of the text.