Chapter 9. LINQ Operators

This chapter describes each of the LINQ query operators. As well as serving as a reference, two of the sections, “Projection” and Joining, cover a number of conceptual areas:

  • Projecting object hierarchies

  • Joining with Select, SelectMany, Join, and GroupJoin

  • Outer iteration variables in query comprehension syntax

All of the examples in this chapter assume that a names array is defined as follows:

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

Examples that use LINQ to SQL assume a typed DataContext variable called dataContext:

var dataContext = new DemoDataContext ("connection string..."); ... public class DemoDataContext : DataContext { public DemoDataContext (string cxString) : base (cxString) {} public Table<Customer> Customers { get { return GetTable<Customer>( ); } } public Table<Purchase> Purchases { get { return GetTable<Purchase>( ); } } } [Table] public class Customer { [Column(IsPrimaryKey=true)] public int ID; [Column] public string Name; [Association (OtherKey="CustomerID")] public EntitySet<Purchase> Purchases = new EntitySet<Purchase>( ); } [Table] public class Purchase { [Column(IsPrimaryKey=true)] public int ID; [Column] public int? CustomerID; [Column] public string Description; [Column] public decimal Price; [Column] public DateTime Date; EntityRef<Customer> custRef; [Association (Storage="custRef",ThisKey="CustomerID",IsForeignKey=true)] public Customer Customer { get { return custRef.Entity; } set { custRef.Entity = value; } } } ...

Get C# 3.0 in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.