Thursday, 9 June 2011

LINQ Basics


LINQ BASICS

Language-Integrated Query (LINQ) is a new feature introduced in visual studio 2008 and .NET Framework 3.5. Developers can write the queries in the code to retrieve the data from various types of data sources like SQL, XML and XQuery.

Earlier developers have to learn the syntax for writing queries for each data source. LINQ simplifies by offering a model to work with the data across various kinds of data sources.

LINQ Architecture

LINQ Flavours


When generating database mappings using LINQ to SQL, which tool allows you to create entity classes using a convenient graphical interface?

The Object Relational Designer (O/R Designer) provides a visual design surface for creating LINQ to SQL entity classes and associations (relationships) that are based on objects in a database
Below is the three advantages of LINQ over stored procedures.

Debugging - As debug point concern, as LINQ is part of .NET, we can use the visual studio's debugger to debug the queries but it is tough to debug the Stored procedure as it will not support the visual studio debugger.

Deployment - In case of deployment, we need to provide an additional script for stored procedures to execute but in case of LINQ, it will complie into single DLL hence deployment becomes easier.

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.

 The disadvantage with LINQ is, it is not a precompiled statement whereas stored procedures are precompiled. In case of LINQ the queries needs to be compile before the execution. So according to this, I can say stored procedures are faster in performance as compared to LINQ.

What are the advantages and disadvantages of LINQ.
Adv. of LINQ:
  • Supports type safety
  • Supports abstraction and hence allows developers to extend features such as multi-threading.
  • Easier to deploy
  • Simpler and easier to learn
  • Allows for debugging through .NET debugger.
  • Support for multiple databases
Disad. of LINQ:
  • LINQ needs to process the complete query, which might have a performance impact in case of complex queries
  • LINQ is generic, whereas stored procedures etc can take full advantage of database features.
  • If there has been a change, the assembly needs to be recompiled and redeployed. 

Can   LINQ  be used with databases other than SQL Server?   

LINQ supports Objects, XML, SQL, Datasets and entities. One can use LINQ with other databases through LINQ to Objects or LINQ to Datasets, where the objects and datasets then take care of database specific operations and LINQ only needs to deal with those objects, not the database operations directly. 

What are the three main components of LINQ or Language Integrated Query?
1. Standard Query Operators
2. Language Extensions
3. LINQ Provider

How are Standard Query Operators implemented in LINQ?
Standard Query Operators are implemented as extension methods in .NET Framework. These Standard Query Operators can be used to work with any collection of objects that implements the IEnumerable interface. A class that inherits from the IEnumerable interface must provide an enumerator for iterating over a collection of a specific type. All arrays implement IEnumerable. Also, most of the generic collection classes implement IEnumerable interface.

What are the steps to use LINQ to SQL in .Net Project.

Steps.

1.      Create a connection to a database

1.       Right-click Data Connections in Server Explorer/Database Explorer and then click Add Connection.
2.       Add LINQ to SQL file.
1.       The Object Relational Designer (O/R Designer) is opened for the DBNAME.dbml file.
3.       To add Table to OR Designer.
1.       Select the table from the data connection and drag it to left pane of OR Designer.
4.       Save the Project
5.       Once you save the project DataContext object for the project. DBNAMEDATACONTEXT gets created.

Following is the code in vb to fill a datagridview.
Dim db As New northwindDataContext

Dim londonCusts = From cust In db.Customers _
                  Where cust.City = "London" _
                  Select cust

DataGridView1.DataSource = londonCusts

1 comment:

Vidhu Sharma said...

Really very significant information for beginners, short concise elaboration..very very helpful.