Back to Basics: Linq and Lambda

Here’s an extra back to basics post. After updating the previous posts with the generics stuff and all those cup of T’s, it’s time for something easy, clean and dirty.
Well, it probably won’t be dirty.

Maybe you’ve noticed how I completely bypass SQL, be it MySQL, MS SQL or whatever. That’s because I don’t like to repeat myself in my code. I like to keep things DRY. That’s why I love Linq. With Linq you can query objects just like how you would query records in a table. With Lambda expressions, you can beautify your code even more.

Bottomline is: at Itslet we want to keep our code simple, elegant and beautiful.
What can I say: we are women. Haha.

Fire up VS and create a Console App!

Let’s create a simple array for an.. animalfarm.

string[] myFarm = {"varken", "koei", "kip", "geit"}

Now the same with the var keyword:

var myFarm = new[] {"varken", "koei", "kip", "geit"}

If you hover over the elements in the array, you notice that they get typed as a string anyhow.

Hover over

A var is not the same as a variant in Visual Basic. In VB we could say: Dim Animal As Variant and then initialize it with Animal = “koei”.
With a var, we need to initialize immediately:

var animal = "koei";

And it will know it’s a string implicitly.

Now let’s query the AnimalFarm with Linq. With Linq we can use queries on almost every collection.

var myFarm = new[] {"varken", "koei", "kip", "geit"}

var beesten = from b in myFarm
                    select b;

foreach (var beest in beesten) {
Console.WriteLine("Er staat een {0} in wei.", beest);
}

This is the output:

Run

Here’s another example of a Linq query:

var myFarm = new[] {"varken", "koei", "kip", "geit"}

var beesten = from b in myFarm
                    where b.Contains("i")
                    select b;

foreach (var beest in beesten) {
Console.WriteLine("Er staat een {0} in wei.", beest);
}

Now if I run the program it will only show the animals with an “i” in its name.

You can perform a whole plethora of Linq methods on the AnimalFarm collection:

3

It’s hard to believe but we can beautify this even further. With Lambda’s. Lambda expressions are the follow-up to delegates from C# 2.0. A Lambda e.g. follows after one of those Linq methods (Contains, Select, Take, Where, Single etc.).

static void Main(string[] args)
 {
//string[] myFarm = { "varken", "koei", "kip", "geit" };
   var myFarm = new[] { "varken", "koei", "kip", "geit" };

//var beesten = from b in myFarm where b.Contains("i") select b;
   var beesten = myFarm.Where(b => b.Contains("i"));

  foreach (var beest in beesten) {
    Console.WriteLine("Er staat een {0} in wei.", beest);
   }
}

See how the Lamda expression is an argument for the Where function? Isn’t that BEAUTIFUL?
Also notice how short and elegant the code is?

Well, I think this is enough awesomeness for today.

You can download the files of this trivial AnimalFarm app here: AnimalFarm

One Reply to “Back to Basics: Linq and Lambda”

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: