This developer's chronicles

Web development using Scrum, C#, AngularJS and the like

Category: ASP.NET

What is the difference between IEnumerable and IQueryable?

IEnumerable and IQueryable may seem like the same at a quick glance, but there are important differences that us, as developers must be aware so that we not only get a working product out the door but also something that performs well.

What is IEnumerable?

Simply put, is something that let’s you go from one element of your collection over to the next (an interface that will provide you with an iterator that moves forward), an example of how you can declare and use IEnumerable is as follow:

As long as you are using a type that implements IEnumerable you will have the advantage of being able to iterate over your collection.

When can I use IEnumerable?

  • When you want to iterate over a collection (List, Arrays, etc.)
  • When you want to get all the results from your query and apply any further filtering once your collection has been loaded in memory.
  •  When you are using LINQ to XML or LINQ to Object.

What is IQueryable?

IQueryable is an interface that implements IEnumerable (and because it implements IEnumerable it means that it allows you to iterate over a collection plus some more)

IQueryable takes Expression objects instead of delegates (which is what IEnumerable takes)

When can I use IQueryable?

  •  When you want to iterate over a collection (same as IEnumerable)
  • When you want to work with data from out-memory, such as a database, a web service etc
  • When you want to apply a filter to your query BEFORE the query is executed (this can save you heaps of resources)
  • When Lazy loading is needed (you only get what you need, not the entire list of records)

So… What’s is the main difference between IEnumerable and IQueryable ?

The main difference is that with IEnumerable you get all the records at once while with IQueryable you only get the records that you want, let’s see that in action with an example:

Let’s assume that you have a client table with all your client’s information (approximately 1000 clients)

  • If you use IEnumerable to get the first 5 customers you end up loading all 1000 and then selecting the first 5.
  • With IQueryable you only select the first 5 (saving a lot of resources !)

For some other useful resources you can check out some stack overflow answers and this dotnet-trick’s article on IEnumerable vs IQueryable

 

Thank you for reading, I hope you have now a clearer idea of IEnumerable and IQueryable and when each of them can be used best.

Understanding delegates and when to use them in C# ASP.NET

This blog post is aimed at beginner developers or someone who have just not fully grasped delegates or don’t really understand what problem do they solve and a good example of when they can be used.

What is actually a delegate?

A delegate is a method signature (pretty much a reference to a method) hence it will have to match the signature of the method it wants to reference, an example below:

Let’s say we have a method called MergeCustomerDetails which takes a couple of strings and returns a string, pretty simple:

Now, if we want to use a delegate to call that method we need to create a delegate whose signature matches the signature of the method (that means that the return type and the parameter types must be the same):

If you see the delegate definition above, it matches the MergeCustomerDetails’ method signature (both return a string, and both take 2 arguments of type string). parameter names don’t have to match though (I’ve used different parameter names to highlight that)

The next step is to initialize the delegate and ‘point’ it to the require method:

So instead of calling MergeCustomerDetails we can call our delegate!

When can I use delegates?

We can use delegates whenever we need to pass methods as parameters to another functions so that they can be executed at runtime from there (although a bit complex to grasp an example may be the best way to understand it).

I found the following example in Stack Overflow where there is an excellent use for delegates, which will be explained below. Let’s see the problem and how delegates helped solve it.

We have three functions with the exact same signature (IsChild, IsAdult and IsSenior), if we wanted to execute any of those methods from inside another method we would have to do it manually as there is no way to pass a method as parameter to another method (without using delegate, that is). That would mean writing almost the same code again and again every time we want to call one of the methods.

Fortunately with the use of delegates we can ‘point’ them to those methods and then pass them as parameters.

Let’s start by defining a Person class and the three methods (note that they all have the same method signature):

Now  let’s define a delegate to see how it matches the methods’ signature:

And the method that will take a delegate as parameter:

Finally let’s add the code to the Main function which will call the ‘DisplayPeople’ method passing all the different methods (IsChild, IsAdult and IsSenior)

 

Thanks, any feedback is appreciated !