Getting to know LINQ

LINQ stands for Language Integrated Query Language. It was a feature introduced along with Visual Studio 2008 that extends the querying capabilities of C# for any sort of data source. Generally LINQ is used to query data sources like SQL Server Databases and XML files. I use it to query databases.

I'm not going to explain everything about LINQ here. I'm going to just talk about some very basic details.

LINQ can be written in two very different ways, syntactically.

  • An expression that looks more like a chain of methods formally knows as Lambda expression
  • An expression that looks like a variant of SQL which could selectively be mixed with the Lambda syntax.

The former method, looks more like the code you are likely to see in a class. The latter looks like you mixed SQL in your application code. Either way behind the scenes, your query would be translated to the most optimal way to fetch data from the data source; now that depends on how you express your requirement in LINQ. LINQ is not magic. You still have to be careful when writing your queries in LINQ. The SQL that gets send to the database (if using LINQ for fetching data from a database) maybe not be exactly what you imagined. So there is a lot of testing to be done, before you can confirm and release your code to production. There are tools like LINQpad which you could use to check the SQL being generated for a given LINQ expression.

I have run into trouble using LINQ when attempting to accomplish something that involved a GROUP BY expression. An SQL query that would take 250 ms was taking abnormally long, at least a minute when I expressed my intentions in LINQ incorrectly. I wasn't born with a LINQ spoon in my mouth. I'm just learning it now.

Apparently there are two types of LINQ expressions, LINQ-to-objects and LINQ-to-entities.

I know giving that link to those websites isn't helping you understanding quickly enough. So let me summarize them for you. The former acts on objects in memory. The latter forces action on the database layer. So if I were to write a complex group by query, I would want to make use of LINQ to entities because databases are the best place to group things up and not do it in memory as lists of objects.

That is just a very short summary. I'll let you decide what you want to do with LINQ.
And for learning LINQ:

Learning LINQ

101 LINQ Examples is a great place to start. So many samples that you will never finish checking out with code and explanation. It starts out with simple queries and then gets to the more challenging ones slowly. 

A few other things to check out might be : 

If you are looking for a tool to help work with LINQ, consider downloading LinqPad. This is an inevitable thing for an application developer working on a project that involves a lot of LINQ. Helps debugging and getting queries behave and obey.  And there are more LINQ tutorials to handle some of the more advanced features in there like Mastering Linq.

comments powered by Disqus