How I learned LINQ query Fundamentals

Some days back, I joined my new organization, and I came to know that my team extensively uses c# for their development purposes and I came from java background. By surfing through internet and watching some videos I was able to grasp basics of c# then started exploring code base my team. I saw that there are quite a lot of code pieces like:




I found it bit difficult to understand as these are not part of c# syntax so after doing some google searches, I found that this is part of something called LINQ(Language-Integrated) queries which provides solution for object-relational mapping and simplifies operation between objects and data sources.

Obviously, I had to learn more about it and become an advanced user, as my team is using it extensively. 

Random thought: Yeah, it's okay I have to learn LINQ queries since my team is using it but why exactly they are using it??

Let's analyze advantages of LINQ:

  • Familiar language: Developers don't have to learn a new query language for every new type of data source or data format. Oh really, that's really cool I have encountered a lot of different kinds of datastores (SQL, NoSQL, historical, etc.), if this LINQ query can bridge the gap and provide me a common interface to query databases this would save a lot of my time and learning efforts too.
  • Less Coding: It reduces the amount of code to be written as compared with a more traditional approach. It's a kind of build up from the first point as LINQ queries are providing a common interface, they would defiantly help in reducing the amount of code to be written.
  • Standardized way of querying multiple sources: Yeah, a build up from point 1.
  • Compile time safety of queries: It provides type checking of objects at compile time. Umm.... not sure of this how it helps?
  • Shaping data: You can retrieve data in different shapes. Interesting...
I feel these advantages are sufficient to go with LINQ queries... I liked 1st point the most and let's now jump into code part of it in order to see these advantages hands on....

Some memory dump before actual code:

To use LINQ queries, we need to use System.Linq namesapce. LINQ queries can be written for classes which implements IEnumerable or IQueryable interfaces as these contains some extension methods which can be used to write LINQ queries. For accessing third party e.g., Amazon, their API's have to implement IQueryable interface.

Snippet: -
Every LINQ query starts with from keyword and ends with Select or GroupBy keyword. 


 Ahh! This looks quite similar to SQL query but yeah, some change in position of variables.

One more way to query:

There is one more way to query data as it is by using methods exposed by IEnumerable or IQueryable interface. Let's see a sample:

Fun fact: 

LINQ query follows delayed execution policy i.e., query would be executed when it has to be used and we can also opt for immediate execution by using operator such as ToList() any other similar operator towards the end of the query.

 Then after this what remains is complex queries which I am planning to learn with time and experience. 



Comments

Popular posts from this blog

Kustov query language

Making all service APi's generic