In this post, I discuss about LINQ to object, an amazing features of C# which is introduced in C# 3.0.
Linq is libraries built by Microsoft to make working with data (for example, a collection of in-memory objects, rows from a database table, or elements in an XML file) simpler and more natural.
LINQ has changed the approach to writing data driven applications. Code is become simpler, often faster, and easier to read.
For demonstrate the approach to coding problem with and without using LINQ, I’ll show example that find out vision how coding practice without LINQ changed with using LINQ.
Example is that there is employee list which is to be sorted accoring to their name and grouped sort by their Department name.
The outputs for both of these example are same. The advantage of using LINQ syntax when working with data from multiple collections, grouping, and aggregating results can clearly how it reduce code and the improved readability.
Benefits of LINQ
Compile-Time Name and Type Checking
LINQ queries are fully name and type-checked at compile-time, reducing runtime error.
Easier to Read Code
The examples shown in this post shows how code perform common tasks with data is simplified. The removal of complex looping, sorting, grouping, and conditional code down to a single query statement means fewer logic errors and simpler debugging.
Over Fifty Standard Query Operators
The built-in set of Standard Query Operators make easy work of grouping, sorting, joining, aggregating, filtering, or selecting data. Visit this MSDN page for reading.
Basic Query Syntax
Simple LINQ query over an integers array to find out even number in sorted order.
Simple LINQ that calculates the sum of all values in an integer array.
We can do these code in Linq’s method chain with lambdas instead of Linq query. for example:
These two syntax perform same operation, because they are compiled into exactly the same code.
Basically query expressions are “pre-processed” by the compiler.
WHICH LINQ SYNTAX TO USE?
Personal preference will win here some choose query syntax while other goes with extension method.
But the main goal is to use the syntax that is easier to read and expose your intention to other programmer who come after you.
- Don’t unnecessarily mix the query syntax with extension methods in one query, it makes query harder to read. After sometimes even you find it harder, and it forces you to count brackets and determine which parts of query the extension method syntax applies.
- If it requires to mixing syntax together, use the query expression at the start surrounded by brackets, then extension methods at the end for necessary operators.
Personally I prefer to use extension method wherever possible, but sometimes have to do in query syntax when need to join, because it easier to write and understand join in query syntax rather than extension method.
Please leave comments about this article…
Next I will publish post for deep understanding of Linq, soon…. till Happy Reading and Coding……..