O'Reilly logo

C# 5.0 in a Nutshell, 5th Edition by Ben Albahari, Joseph Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

  • 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 typed DataContext 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required