Query Composition using Functional Programming Techniques in C# 3.0

Functional Programming (FP) has the potential to reduce program line count by 20% to 50%, reduce bugs and increase robustness, and move us in the direction of taking advantage of multiple core CPUs.  But perhaps due to the learning curve, and unfamiliarity, many developers are not taking advantage of the potential that LINQ and FP offers.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC
This is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries.

It is certainly possible to write simple LINQ queries without using these techniques, but as soon as you start writing more complicated queries, you need to understand these techniques.

Learning about functional programming made me a better and faster coder.

The tutorial is written in a granular fashion so that if you already understand a topic, you can skip it.  Feel free, for instance, if you have firm grasp on lambda expressions and extension methods to skip those topics.  At the end of each topic is a link to the next, or you can navigate using the table of contents.

1. Introduction to the FP Tutorial

Presents the case for why you want to learn functional programming.

2. The Main Scenario for Functional Programming

Functional programming is most applicable when transforming data.

3. What this Tutorial Covers

Gives a quick overview of what the tutorial covers (and doesn't).

4. Contrast of FP to Traditional Object-Oriented Programming

FP is about transformations.  OOP is about class design and algorithms. 

5. An Example Presented in Both Coding Styles

Presents example code to manipulate an (artificially simplified) image.

6. Quick Intro to Query Expressions

Capsule summary of query expressions and explicit notation.  Answers the question, "What is a query?"

7. Lambda Expressions

Introduction to lambda expressions, their syntax, and their semantics. Lambda expressions make functional programming fun.

8. Extension Methods

 Introduction to extension methods, and their applicability to functional programming.

9. Local Variable Type Inference

Anonymous types are key to FP, and in certain circumstances, to use anonymous types, you must use local variable type inference.

10. Object and Collection Initializers

To use anonymous types, you must use object and collection initializers.

11. Projection

A transformation is an operation of projection.

12. Tuples and Anonymous Types

Tuples are important for creating intermediate values (and sometimes final results) of queries. This introduces tuples as implemented via anonymous types. 

13. Using Nominal Types for Tuples

Nominal types allow for greater ease with refactoring. 

14. The Yield Contextual Keyword

Yield blocks are the foundation on which lazy evaluation is based. Yield blocks were introduced to C# in version 2.0. 

15. Lazy Evaluation

This introduces lazy evaluation, and in contrast, eager evaluation.

16. Deferred Execution

Deferred execution is related to lazy evaluation, but the two terms are not synonymous. 

17. Aggregation

Aggregation is a core tool that you will use to develop results when using FP. 

18. Grouping

Queries can project hierarchical result sets (in contrast to SQL, which projects only rectangular result sets). 

19. Pure Functions

Refactoring in FP consists of creation of pure functions. This defines pure functions and tells why you want to use them. 

20. Programming in a Functional Style

Introduces the idea of declarative programming (vs. imperative programming).

21. Parsing XML from an Open XML Document

This introduces our main task that we'll accomplish in this tutorial. This example pulls together all of the previously introduced topics, including lambda expressions, extension methods, tuples (including type inference and object initializers), lazy evaluation, aggregation, and pure functions.

22. The Source Open XML Document

Shows the document that we'll be working with.

23. Printing the Parts of an Open XML Document

Small utility program to extract the XML in parts so that you can see it more easily.

24. Using the Open XML SDK

The Open XML SDK makes it easier to work with Open XML documents.

25. Retrieving the Paragraphs

Develops a query that returns all of the paragraphs in a Word document. Creates an intermediate result using an anonymous type.

26. Coding Style

Shows a code formatting approach that sometimes yields more readable code.

27. Refactoring using a Pure Function

Refactors the previously introduced query using a pure function. The resulting query is easier to read.

28. Retrieving the Default Style Name from the Styles Part

Extends the previous example to make it more correct.

29. Retrieving the Text of the Paragraphs

Modifies the query to retrieve the text of each of the paragraphs. Adds the text to the tuple.

30. Retrieving the Two Code Groups

Introduces a new query operator, GroupAdjacent, implemented as an extension method on IEnumerable<T>.

31. Filtering Out the Groups that we Don't Want

Modifies the query to retrieve just the groups that we want.

32. The Final Results

Modifies the query to retrieve exactly what we want. This is the end target of this example.

33. Complete Listing of ParseWordML

Contains the complete listing of our example.

34. Conclusion

Final thoughts about functional programming.


Using Annotations to Transform LINQ to XML Trees in the style of XSLT.

LINQ Reduces Line Counts and Makes Code "Pop".

Using LINQ to XML to Access Open XML Documents.

An Explanation of Closures.

[Blog Map]

Comments (33)
  1. Since everyone else does it, I figured I&#39;d start linking as well: Keith Ray on the downsides to mocking

  2. Pentru cei interesaลฃi de LINQ, Eric White, developer în echipa de XLINQ a scris o serie de articole în

  3. Lazy Coder says:

    This is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries….

  4. Here are a few interesting blog posts. Eric White’s Functional Programming Tutorial is a few months old

  5. Wriju's BLOG says:

    I found a very nice link on C# 3.0 resources and sharing it for you. Query Composition using Functional

  6. Adrian. says:

    This is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries. It is certainly possible to write simple LINQ queries without using these techniques, but as soon as you start writing more complicated queries, you need

  7. Chua Wen Ching says:

    Very good stuff. Keep it up ๐Ÿ™‚

  8. dpwebs says:


    can you publish this entire tutorial as pdf file? It is excellent (on first look)!


  9. Steve says:

    I agree that a PDF version would be rockin! ๐Ÿ™‚

  10. I agree, a PDF would be good.  However, for a variety of reasons, I can’t do it right now.  Hopefully in the not too far future.  ๐Ÿ™‚


  11. dpwebs says:

    I still agree that PDF version would be highly appreciated…

  12. Hier ein Link zu einem interessanten Blog Eintrag: Query Composition using Functional Programming Techniques

  13. You’ve been kicked (a good thing) – Trackback from DotNetKicks.com

  14. Sam says:

    These are awesome–great link post ๐Ÿ™‚

  15. Query Composition using Functional Programming Techniques in C# 3.0

  16. I had an interesting conversation with my nephew the other day. He is a very bright CS student working

  17. shahzad sarwar says:

    Really a Great article, Very rich in thoery n concepts…. thanks for it …

  18. shahzad sarwar says:

    Secondly , i have to read each line many times to understand the backgroud….. Really a rich guide.

  19. Thanks for the kind words, Shahzad.  I’m glad it’s useful.  ๐Ÿ™‚

  20. Closures are one of the key components in C# 3.0 that makes functional programming easy, and results

  21. Transforming Open XML documents using XSLT is an interesting scenario, but before we can do so, we need

  22. When developing C# programs in the functional programming style, you often need to dump out a collection

  23. rob says:

    Hey man,

    great blog, some really good information im booking marking this and coming back a lot more.


  24. Cesa says:

    Very useful tutorial, thanks. It would be nice to have a printed version, is there any hope that there will be a pdf version?

  25. Still trying to get you head around Linq and wondering what all the interest in Functional Programming

  26. Visual Basic 9.0 added many language features that allow us to write in the functional style in a natural

  27. In LINQ in Action , we discuss about the missing ForEach query operator. This is in Chapter 5 "Beyond

  28. In LINQ in Action , we discuss about the missing ForEach query operator. This is in Chapter 5 "Beyond

  29. dev1 says:

    Great tutorial Eric, will be super if pdf version is available.

  30. Hi dev1, I’ll see what I can do about that.  I understand it would be good to take off-line, also to print.


  31. Sriram says:

    You are a god. I bow down to you. Just what I was looking for… Thank you.

  32. Frederic says:

    Hello Eric,

    Many thanks for that really helpful work, i can imagine the hours need to build that tutorial. Very interesting.

    And indeed a PDF can be a nice to have for me to.

Comments are closed.

Skip to main content