Query Operator Overview

The sections that follow describe each of the LINQ query operators, as summarized in Table 1-1.

Table 1-1. LINQ query operators

Category

Operators

Filtering

Where, Distinct, Take, TakeWhile, Skip, SkipWhile

Projecting

Select, SelectMany

Joining

Join, GroupJoin

Ordering

OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse

Grouping

GroupBy

Set

Concat, Union, Intersect, Except

Conversion import)

OfType, Cast

Conversion export)

ToArray, ToList, ToDictionary, ToLookup, AsEnumerable, AsQueryable

Element

First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault, ElementAt, ElementAtOrDefault, DefaultIfEmpty

Aggregation

Aggregate, Average, Count, LongCount, Sum, Max, Min

Quantifiers

All, Any, Contains, SequenceEqual

Generation

Empty, Range, Repeat

The examples 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();

	...
	
	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 stringName;

	  [Association(OtherKey="CustomerID")]
	  public EntitySet<Purchase>Purchases = new EntitySet<Purchase>(); } ...

Get LINQ Pocket Reference 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.