Skip to Content
LINQ Pocket Reference
book

LINQ Pocket Reference

by Joseph Albahari, Ben Albahari
February 2008
Intermediate to advanced
172 pages
3h 29m
English
O'Reilly Media, Inc.
Content preview from LINQ Pocket Reference

Lambda Queries

Lambda queries are the most flexible and fundamental. In this section, we describe how to chain operators to form more complex queries and introduce several new query operators.

Chaining Query Operators

To build more complex queries, you add additional query operators, creating a chain. For example, the following query extracts all strings containing the letter a, sorts them by length, and then converts the results to uppercase:

	string[] names = { "Tom","Dick","Harry","Mary","Jay" };

	IEnumerable<string> query = names
	  .Where   (n => n.Contains ("a"))
	  .OrderBy (n => n.Length)
	  .Select  (n => n.ToUpper( ));

	foreach (string name in query)
	  Console.Write (name + "|");

	// RESULT: JAY|MARY|HARRY|

Where, OrderBy, and Select are all standard query operators that resolve to extension methods in the Enumerable class.

We already introduced the Where operator, which emits a filtered version of the input sequence. The OrderBy operator emits a sorted version of its input sequence; the Select method emits a sequence where each input element is transformed or projected with a given lambda expression (n.ToUpper(), in this case). Data flows from left to right through the chain of operators, so the data is first filtered, then sorted, then projected.

Note

A query operator never alters the input sequence; instead, it returns a new sequence. This is consistent with the functional programming paradigm, from which LINQ was inspired.

Here are the signatures of each of these extension methods (with the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

LINQ For Dummies®

LINQ For Dummies®

John Paul Mueller
Essential LINQ

Essential LINQ

Charlie Calvert, Dinesh Kulkarni

Publisher Resources

ISBN: 9780596519247Errata Page