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

Subqueries

A subquery is a query contained within another query’s lambda expression. The following example uses a subquery to sort musicians by their last name:

	string[] musos =
	  { "David Gilmour","Roger Waters", "Rick Wright" };

	IEnumerable<string> query =
	  musos.OrderBy (m => m.Split().Last());

m.Split converts each string into a collection of words, upon which we then call the Last query operator. Last is the subquery; query references the outer query.

Subqueries are permitted because you can put any valid C# expression on the right side of a lambda. A subquery is simply another C# expression, meaning that the rules for subqueries are a consequence of the rules for lambda expressions (and the behavior of query operators in general).

A subquery is privately scoped to the enclosing expression and is able to reference the outer lambda argument (or iteration variable in comprehension syntax).

Last is a very simple subquery. The next query retrieves all strings in an array whose length matches that of the shortest string:

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

	IEnumerable<string> outerQuery = names
	  .Where (n => n.Length ==
	     names.OrderBy (n2 => n2.Length)
	          .Select  (n2 => n2.Length).First( )
	  );

	// RESULT: Tom, Jay

Here’s the same thing in comprehension syntax:

	IEnumerable<string> comprehension =
	  from     n in names
	  where    n.Length ==
	    (from n2 in names
	     orderby n2.Length
	     select n2.Length).First( )
	  select n;

Because the outer iteration variable (n) is in scope for a subquery, we cannot ...

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