tailieunhanh - C# in Depth what you need to master c2 and 3 phần 9

Rõ ràng, trình biên dịch cần phải làm việc ra loại mảng để sử dụng. Nó bắt đầu bằng cách hình thành một tập hợp chứa tất cả các loại thời gian biên dịch của các biểu thức bên trong Ngầm gõ mảng C # 3 cho phép một cái gì đó ở giữa: | Groupings and continuations 307 In just five lines of code we have retrieved parsed and filtered a whole collection of log files returning a sequence of entries representing errors. Crucially we haven t had to load even a single full log file into memory all in one go let alone all of the files all the data is streamed. Having tackled joins the last items we need to look at are slightly easier to understand. We re going to look at grouping elements by a key and continuing a query expression after a group . by or select clause. Groupings and continuations One common requirement is to group a sequence of elements by one of its properties. LINQ makes this easy with the group . by clause. As well as describing this final type of clause we ll also revisit our earliest one select to see a feature called query continuations that can be applied to both groupings and projections. Let s start with a simple grouping. Grouping with the group . by clause Grouping is largely intuitive and LINQ makes it simple. To group a sequence in a query expression all you need to do is use the group . by clause with this syntax group projection by grouping This clause comes at the end of a query expression in the same way a select clause does. The similarities between these clauses don t end there the projection expression is the same kind of projection a select clause uses. The outcome is somewhat different however. The grouping expression determines what the sequence is grouped by the key of the grouping. The overall result is a sequence where each element is itself a sequence of projected elements and also has a Key property which is the key for that group this combination is encapsulated in the IGrouping TKey TElement interface which extendsIEnumerable TElement . Let s have a look at a simple example from the SkeetySoft defect system grouping defects by their current assignee. Listing does this with the simplest form of projection so that the resulting sequence has the

TỪ KHÓA LIÊN QUAN