Chapter 9. LINQ Operators

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

  • Projecting object hierarchies

  • Joining with Select, SelectMany, Join, and GroupJoin

  • Query expressions with multiple range variables

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 query a database assume that a variable called dataContext is instantiated as follows:

var dataContext = new NutshellContext ("connection string..."); ... public class NutshellContext : DataContext { public NutshellContext (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 ...

Get C# 6.0 in a Nutshell, 6th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.