Monday, January 6, 2014

LINQ - Part 1

What is LINQ?

LINQ is an acronym for Language Integrated Query, which is descriptive for where it's used and what it does.
  • The Language Integrated part means that LINQ is part of programming language syntax. In particular, both C# and VB are languages that ship with .NET and have LINQ capabilities.
  • The other part of the definition, Query, explains what LINQ does. LINQ is used for querying data. Notice that I used the generic term "data" and didn't indicate what type of data. That's because LINQ can be used to query many different types of data, including relational, XML, and even objects.
  • In Simple Terms, LINQ is that it is programming language syntax that is used to query data.
LINQ enables us to perform complex query operations against any enumerable object (object that supports the IEnumerable interface)

What are the Advantages of LINQ?
  • Its a declarative style of programming, hence the code is simpler and lesser.
  • LINQ in .NET enables developers to write code in a declarative format. Lets see the Practical advantage and implementation of LINQ.
 
What I am trying to do here is, find all the numbers in the array "nums", where the number is less than 5. Then I want to order those numbers in ascending order. What is shown below is the traditional way of imperative programming.

int[] nums = { 5, 4, 3, 9, 8, 6, 7, 2, 0 };
int[] small=new int[nums.Length];
int y = 0;

int length = nums.Length;

for (int i = 0; i < length; i++)
{               
    if (nums[i] < 5)
    {
        small[y]=nums[i];
        y++;
    }               
}

Array.Resize(ref small, y);

Array.Sort(small);

for (int i = 0; i < small.Length;i++ )
{
    Console.WriteLine(small[i]);
}
The output of the above code would be 0 2 3 4
If we write the same code using LINQ which is a declarative style of programming, this is how the code would look like this :

int[] nums = { 5, 4, 3, 9, 8, 6, 7, 2, 0 };

var small = from n in nums where n < 5 orderby n select n;

foreach (var x in small)
{
    Console.WriteLine(x);
}

As you can see above, in a declarative style of programming, you don't need to worry about how it gets done (I mean the implementation) . You just tell the compiler, this is what you want and the compiler gets it done for you.

2. LINQ provides nearly identical syntax for querying many different data sources such as Collections, Objects, SQL Server etc. Hence you don't have to learn a new syntax for querying every different form of a data source. You can just use LINQ everywhere.(Note LINQ to XML which is significantly different is an exception)

3. Another Practical Scenario where LINQ can be used.

Consider a scenario where you need to bind 3 columns to the Grid View.

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField HeaderText="Employee No" DataField="Eno" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
</Columns>
</asp:GridView>

If you want to sort the data based on the user selection, whenever the user clicks you need to call some Stored Procedure to get the sorted data. If you use LINQ, you can do that sorting without hitting the database (if you persist the data at the time of retrieval).
 
GridView1.DataSource = from emp in Employees
orderby emp.firstname select emp;

4. Type Safety - As LINQ is type safe, the queries errors are type checked at compile time. Better suggest to use LINQ because it helps to encounter an error at the compile time rather than at runtime exception.
 
5. Debugging - From Debug point of view, as LINQ is part of .NET, we can use the visual studio's debugger to debug the queries. 

 
What are the Disadvantages of LINQ ?

LINQ queries need to be compiled before the execution. There might be a performance issue because of this. 

1 comment:

  1. Casino games - JTGHub
    Online 당진 출장안마 Casino games for real money can be 천안 출장마사지 found on the casino floor, or on the floor, across 사천 출장마사지 the board, in the slot machines and table 인천광역 출장마사지 games 문경 출장안마 and live dealer games.

    ReplyDelete