Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

Writing Operators

Modified: 2008/05/22 04:18 by mwatts - Categorized as: LINQ to Objects, Samples
Edit

LINQ to Objects

LINQ to Object operators are simply extension methods on any type that implements IEnumerable<T>.

Writing a new LINQ to Object query operator sounds more complicated than it actually is. The standard query operators supplied by Microsoft rarely span more than a few lines of code each. The operators make heavy use of the introduced language features in C# 2.0 (Released as part of Visual Studio 2005), especially the yield return keyword which makes writing iterators easier. The first port of call when wanting to understand how to write an operator is to look at the source code for the Standard Query Operators. Since the release of version 3.5 of the framework, the implementation of System.Linq.Enumerable found in the System.Core assembly is where most of the Standard Query Operators can be found. Reflector can be used to view a decompilation of the released assembly.

The query operators fall into a few different categories, and the implementation pattern for each of these differ.

1. Operators that return a Single Element. E.g. First, Last.

2. Operators that return a Sequence. E.g. Where, Select.
See Between Operator Sample

3. Operators that return an Aggregate result. E.g. Count, Min, Max, Average.

4. Operators that return groupings. E.g. GroupBy, GroupJoin
See Segment Operator Sample

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.