Getting started with LINQ

Hi Friends,

In today’s section, we are going to talk about LINQ and its capabilities. Now, LINQ is something which gives you out of the box querying facility in statically typed language itself means in C# itself. Let’s see some of the examples in action. Below, I have created one simple movie class with few properties in it.

however, when i am writing the query in the main program, you can see that intellisense is giving me all the properties which i have created above.

46th

when i execute the same, it prints the below value.

47th

However, there are around 50 standard operators provided by microsoft to query the things. You can check each implementation on MSDN as well. Also, LINQ is designed in such a way that it supports all kind of extensibility like you can write LINQ to LDAP, Amazon, Flicker or you can write your own custom operators as well.

However, we can also go ahead and use LINQ to query objects like shown in the below snippet

so, it produced me the Movie type as public type.

48th

Deferred Execution is again one of the most crucial part of LINQ operations means we can have the query defined, but until we invoke it that query part is not going to be executed. Consider the below example where in i am adding a new movie to list and then invoking the query from the foreach() operation. So, in the final result it will print me the expected value as shown below.

49th

I can also use LINQ to query XML like shown below. So, here i have constructed one xdocument on the fly of processes and querying the same via LINQ.

51th

then, i can see the result in the debugging visualizer as shown below.

50th

Now, since the document is constructed i can go ahead query the document as well as shown below in the snippet.

52nd

This was the brief introduction of using LINQ with variety of instances. we’ll delve further inside this in the coming sections. Till then stay tuned and Happy Coding.

Thanks,
Rahul Sahay
Happy Coding

IEnumerable

Hi Friends,

Today in this section, we’ll see IEnumerable in action and will see that how it actually works and holds true for all collection. As i already said in one of my posts that collection don’t iterate itself, they need enumerators to loop through and its IEnumerable‘s job to provide the enumerators. so, whenever we loop any collection under foreach(), we are actually invoking IEnumarable to get the enumerator. So, IEnumerable exposes only one method and that is GetEnumerators().

Here, enumerator uses three basic properties to iterate in the list and that is MoveNext(),reset and Current. MoveNext() will iterate till the collection ends and Current will set the subsequent item to current. Now, let’s see the same in action.

32nd

Now, consider the scenario of foreach(). As shown below in the example foreach() is pretty straight forward, i could have used the same in main method directly as i was using in the previous examples. However, i just wanted to emphasize on one point that foreach() internally replaces the code with with the previous example of IEnumerator.

and offcourse result will be the same only.

32nd

Now, there is one exception to this, if the compiler notices that collection which we are iterating through is Array, then it will simply convert the foreach() method to to for loop than IEnumeartor implementation, as this is the most efficient way of iterating through. But, this only holds for arrays.

One more point is, enumeration is read only operation, so if you tries to modify the collection on the fly and iterate through, this won’t allow.

45th

However, the above restriction is not applicable for all the collections. For example, if i change the same to Array and run the again, then compiler won’t complain. However, one point to note that its not a good practice to modify the element while iterating and the reason for this is foreach() for arrays gets converted into for loop, hence modification allowed in that.

Thanks for joining me.

Rahul sahay,
Happy Coding