Translate

Wednesday, December 14, 2011

What is LINQ?


Traditionally C# (like C++, java etc) has been an imperative programming language. That means if you want to fetch data from a source or dataset, we have to give the compiler a detailed set of precise instructions. For instance, suppose I have a datatable that has information on all my employees. If I want to find the salary of an employee named John, I would have to write code to loop thru every row in the datatable.
 Now suppose the same table is there in a SQL database. If I need to find the salary of John, all I would do is write something like

Select salary from tblEmployees where firstName='John'

As you can notice, in SQL I don't have to write code to loop through all the rows in the table.  DBMS does the job of looping through for me. This way of coding where you just declare what you want and the job gets done behind the scene by some engine for you, is called declarative programming.

 So what we have learned above can be summed up as

Imperative programming:  :  Give detailed steps to get required data. eg C#,C++ etc
Declarative programming (functional programming)  :  Tell the computer what you need and it will be fetched for you. eg SQL

Clearly, compared to imperative programming,  declarative programming requires a lot lesser code to retrieve data. LINQ is the way to write declarative code in C#.

No comments:

Post a Comment

Comments will appear once they have been approved by the moderator