Skip navigation

LINQ Unleashed for C#

Book review by Zeeshan Hirani

BookReport

 

LINQ Unleashed for C#

LINQ Unleashed for C# by Paul Kimmel takes great strides in covering all areas of LINQ, from core basics such as extension methods, query syntax, automatic property, and collection initialization to domain-specific implementation of LINQ such as LINQ to SQL, LINQ to Objects, and LINQ to XML. The comprehensive text is accompanied by copious examples explaining each concept in detail. The introductory chapters are great for beginners, but later chapters demonstrate advanced concepts that can be immediately applied in real-world applications.

Chapter 1 starts off the book with the introduction of anonymous types and how the compiler generates a class for it under the covers. An example of anonymous types is demonstrated by returning projections from a LINQ query.

Chapter 2 discusses how to initialize a collection at the time of declaration, which is simpler than in prior code in .NET 2.0, where we had to define a collection and then add objects to it afterward. Here we also are introduced to new conversion operators, such as ToArray, OfType, Cast, and AsEnumerable, with examples of how to use each one.

Chapter 3 provides an overview of extension methods and how they allow extending sealed classes with methods that aren't part of the class. This chapter also explains how LINQ (under the covers) uses extension methods to extend collection classes such as IEnumerable<T>, IQueryable<T>, and others. We're also introduced to partial methods and how they serve as placeholders for code generated by designers, such as in LINQ to SQL.

Chapter 4 introduces yield return and yield break statements by explaining how LINQ generates enumeration code, which developers had been writing in the days of .NET 1.0 and 1.1. Although the yield keyword serves a great purpose in the language by itself, LINQ queries written on top of it offer much higher abstraction for querying and filtering records.

Chapter 5 goes in to great depth about how lambda expressions evolved, starting from delegates and anonymous methods. Examples are provided of how to use built-in delegates, such Func<T>, Predicate<T>, and Action<T>. The last part of the chapter shows uses of projections, closures, and automatic properties, introduced in .NET 3.0.

Chapter 6 explores the standard query operators available in the .NET Framework, and which operators can be invoked by using only extension methods. Examples are shown of how to filter collections, from using the where clause to using the OfType operator to filter object types. Use of the Any and All operators to test whether an item or all items meets a condition is also demonstrated. The last part of this chapter presents the use of Skip, SkipWhile, Take, and TakeWhile to partition a collection.

Chapter 7 explores the sorting and grouping capabilities of LINQ. Kimmel explains with examples how LINQ keywords such as order by and order by descending get translated to OrderBy and OrderByDescending extension methods. This chapter also provides examples of using the group by keyword in a LINQ query to organize data in a logical group and printing each group to the console window.

Chapter 8 provides examples of applying aggregate operators, such as Min, Max, Sum, and Count. Some of the more interesting examples include calculating the sum of integers and calculating average file size. Using aggregate operators with IComparable to compare specific properties in an object also is shown. The Let keyword also is introduced to store the result of sub expressions that could later be used in a query.

Chapter 9, Performing Set Operations, shows the use of Distinct, Intersect, Except, and Union operators, using examples such as finding common files between two folders, distinct cities customers belong to, and the union of even and Fibonacci series. Also demonstrated is using IEqualityComparer with Distinct, Intersect, and Except operators to compare specific properties of an object.

Chapter 10 delves in to explaining how to use the Select operator in a LINQ query as well as how to use the Select extension method to get access to the index of an item in the list. To demonstrate this, Kimmel uses the Select operator in a blackjack game, converting the first letter to upper case. Overall, this chapter shows plenty of real-world uses of the Select and SelectMany operators in LINQ.

Chapter 11 deals with how to use various types of joins inside a LINQ query. The examples in this chapter start with a simple inner join, then proceed to more complicated joins, like nonequijoins, left joins, cross joins, group joins, and full outer joins. The last example in the chapter reveals how to join two collections, such as customer and orders, on multiple keys by using anonymous types in C#.

Chapter 12 explores how LINQ can be extended to other domains. Examples of reading contact lists from Microsoft Outlook and reading information from Active Directory demonstrate that LINQ can be applied anywhere, as long as the provider can convert the expression tree to its native language.

Chapter 13 introduces how to use LINQ to query relational data. The discussion covers two main approaches to querying data; DataSet and LINQ to SQL. With the DataSet approach, the use of Field and AsEnumerable operators is discussed. For LINQ to SQL, the use of a designer and command line utility to generate the model is discussed.

Chapter 14 discusses various attributes in LINQ to SQL, such as Column, Table, and CanBeNull. Implementing inheritance using a discriminator column also is shown. The last part of the chapter discusses various API methods in LINQ to SQL, such as CreateDatabase and DeleteDatabase, which can create and drop a database.

Chapter 16 shows how to perform CRUD operations, such as insert, update, and delete, using LINQ to SQL. There also is a section covering stored procedures, which explains how stored procedures can be used in LINQ to SQL to customize the insert, update, and delete processes. Use of functions to query data also is shown. The chapter ends with a discussion of how to use LINQ to SQL in n-tier architecture, such as Windows Communication Foundation (WCF) services.

Chapter 17, the Entity Framework chapter, begins with the introduction of how to build models using the wizard. Explanation is provided to differentiate Entity Framework from other ADO.NET technologies, such as LINQ to SQL, n-hibernate, and raw ADO.NET code. An example of using stored procedures is provided by building a small application that reads stock quotes from Yahoo!.

Part IV of the book deals with manipulating data in XML using the XDocument and XElement classes. Readers are introduced to concepts about namespaces as well as how to remove elements, add new attributes, and assign new value to an element. Chapter 19 goes into depth about comparing XML with other technologies. Chapter 21 shows how to use XmlWriter to create new XmlDocuments, and chapter 22 presents a real-world example of creating XML documents using data read from the database. Chapter 23 explores the new territory of LINQ to XSD by showing some of its benefits, such as strongly typed access to the Document Object Model (DOM) structure.

Overall, LINQ Unleashed for C# does a fabulous job of covering C# 3.0 concepts, such as anonymous methods, lambda expressions, and query syntax and extension methods. The later part of the book uses these concepts and applies them in various domains, such as LINQ to Objects and LINQ to SQL.

 

Zeeshan Hirani

 

Rating:

Title: LINQ Unleashed for C#

Author: Paul Kimmel

Publisher: SAMS

ISBN: 0-672-32983-2

Web Site: http://www.informit.com

Price: US$49.99

Page Count: 552

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish