EditOverview
LINQ to Objects allows .NET developers to write “queries” over collections of objects. Out of the box there is a large set of query operators that provide a similar depth of functionality to what we expect from any SQL language working with a relational database, and if what we need isn’t present out-of-the-box, we can add our own.
Traditionally, working with collections of objects meant writing a lot of looping code using for loops or foreach loops to iterate through a list carrying out filtering using if statements, and some action like keeping a running sum of a total property. LINQ frees you from having to write looping code; it allows you to write queries that filter a list or calculate aggregate functions on elements in a collection as a set.
We can write queries against any collection type that implements an interface called IEnumerable (and also a new interface called IQueryable, but more on that later). This is almost any collection type built into the .NET class libraries including simple arrays like string[], or int[], and any List<T> collection we define.
Features at a glance:
- Query language over collections of objects
- Standard SQL like set of query operators, including joins and grouping
- New query operators can be added, fully extensible
- Intellisense and compiler syntax checking support in Visual Studio
LINQ to Objects - 5 minute overview
EditStandard Query Operators
The backbone of LINQ to Objects is the
Standard Query Operators that provide query operators similar to those found in a SQL like language. You can also
Write you own operators, pretty easily if one of these doesn't suit your needs.
| Operator Type | Operator Name |
| Aggregation | Aggregate, Average, Count, LongCount, Max, Min, Sum |
| Conversion | Cast, OfType, ToArray, ToDictionary, ToList, ToLookup, ToSequence |
| Element | DefaultIfEmpty, ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault |
| Equality | EqualAll |
| Generation | Empty, Range, Repeat |
| Grouping | GroupBy |
| Joining | GroupJoin, Join |
| Ordering | OrderBy, ThenBy, OrderByDescending, ThenByDescending, Reverse |
| Partitioning | Skip, SkipWhile, Take, TakeWhile |
| Quantifiers | All, Any, Contains |
| Restriction | Where |
| Selection | Select, SelectMany |
| Set | Concat, Distinct, Except, Intersect, Union |
LINQ 101 Samples - This Microsoft link is to the source-code for 100 sample snippets showing how to use the Standard Query Operators.
EditResources