O'Reilly logo

LINQ Pocket Reference by Joseph Albahari, Ben 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

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>(); } ...

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