First of all, we need to understand the concept of pipelines and filters, Wikipedia says:
Pipeline (computing)
Instruction scheduling on the Intel Pentium 4.
In computing, a pipeline is a set of data processing elements connected in series, so that the output of one element is the input of the next one. The elements of a pipeline are often executed in parallel or in time-sliced fashion; in that case, some amount of buffer storage is often inserted between elements.
Filter (higher-order function)
In functional programming, filter is a higher-order function that processes a data structure (typically a list) in some order to produce a new data structure containing exactly those elements of the original data structure for which a given predicate returns the boolean value true.
Basically we can create a pipeline with filters resulting in some final product we want.
Let’s make it clear:
We have a class called PC with the properties LCD (boolean) and WiFi(boolean):
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PipeLinesAndFilters
- {
- public class PC
- {
- public bool LCD { get; set; }
- public bool WiFI { get; set; }
- }
- }
We have 3 PCS:
| PC | LCD | WiFi |
| 1 | true | true |
| 2 | true | false |
| 3 | false | false |
We code this:
- PC pc1 = new PC();
- pc1.LCD = true;
- pc1.WiFI = true;
- PC pc2 = new PC();
- pc2.LCD = true;
- pc2.WiFI = false;
- PC pc3 = new PC();
- pc3.LCD = false;
- pc3.WiFI = false;
We put this pcs into a list
- List<PC> lstPcs = new List<PC>();
- PC pc1 = new PC();
- pc1.LCD = true;
- pc1.WiFI = true;
- lstPcs.Add(pc1);
- PC pc2 = new PC();
- pc2.LCD = true;
- pc2.WiFI = false;
- lstPcs.Add(pc2);
- PC pc3 = new PC();
- pc3.LCD = false;
- pc3.WiFI = false;
- lstPcs.Add(pc3);
So now we start working with pipelines and filters.
We will create extension methods(Here’s my post about extension methods) so we can filter this list, and then we’ll be able to do something like that:
- lstPcs.WithLCD(true);
Or
- lstPcs.WithWiFi(false);
Now we must create our PCFilters class and implement our extension methods.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PipeLinesAndFilters
- {
- public static class PCFilters
- {
- public static List<PC> WithLCD(this List<PC> lstPCS, bool lcd)
- {
- }
- public static List<PC> WithWiFi(this List<PC> lstPCS, bool wifi)
- {
- }
- }
- }
Ok, we have our methods, but we need to return something, and that is our primary list List<PC> lstPCS filtered.
- public static class PCFilters
- {
- public static List<PC> WithLCD(this List<PC> lstPCS, bool lcd)
- {
- return lstPCS.FindAll(p => p.LCD == lcd);
- }
- public static List<PC> WithWiFi(this List<PC> lstPCS, bool wifi)
- {
- return lstPCS.FindAll(p => p.WiFI == wifi);
- }
- }
Now you can try filter our primary list using this extension methods (our filters) and create pipelines with this filters resulting in anything you want:
- lstPcs.WithLCD(true).WithWiFi(false);
- lstPcs.WithWiFi(true).WithLCD(true);
Ok! Our Pipelines and filters works like a charm
It may sound stupid when you have a class with 2 properties, but it can make your code really clear when you have a big project with hundreds of classes and properties.
Get your logic classes away from LinqToSql queries is nice
PS1 – Let your comments and spread the word, publish my post in your social media networks if you like.
PS2 – Feel free to find me on your social networks using the right bar!
Thanks everyone and cya!

[...] This post was mentioned on Twitter by Felipe P Gentil and Talita Facirolli, Caspar Kleijne. Caspar Kleijne said: RT @stJhimyBlog: .NET Pipelines and Filters! – http://bit.ly/bmJ3Ey #dotnet #csharp #programming #development [...]