Sunday, 6 November 2016

LINQ (Language Integrated Query) Part-2

LINQ (Language Integrated Query) Part-2

Microsoft in 3.5 framework introduce LINQ (Language Integrated Query). It is given as a query language which we can write a query on wide variety of data sources. like we can write queries on Relational databases, XML files, arrays, Collections.
Before LINQ we have SQL (Structure Query Language) and we can write the query for Relational Database. but in the LINQ we can write the query for wide variety of data sources.

So,LINQ is basically you can divide in different categorisation. These are as follows:

 =>   LINQ to Objects 
 =>   LINQ to Database
 =>   LINQ to XML

LINQ to Objects
   Using this category of LINQ we can write the query on array, collection etc.

LINQ to Databases
   Using this category of LINQ we can write the query on Data tables, Relational Database tables i.e. 
      - LINQ to ADO.Net
      - LINQ to SQL
      - LINQ to Entities 

LINQ to XML
  Using this category of LINQ we can write the query on XML files.

Now i am going to discuss about LINQ to SQL.

Linq to Sql
  
     It is a query language which is introduce in .Net 3.5 framework for working with relational database i.e. Sql server only for Sql server relational database not for other relational databases.
Linq to Sql is not only about querying the data but also us to perform CRUD operation i.e. Insert, Update, Delete operation.
CRUD means :- [Create (insert), Read (Select),Update, Delete].

Note :- We can also call the Stored Procedure by using Linq to Sql.

And last one Linq to Entities : Using this Linq to Entities we can write the query on Sql server relational databases as well as Other relational databases like Oracle, etc...



Now Let's People raise the Question here :

=>We have already a query language which is called SQL server and using ADO.Net we interact with database. Then why LINQ again. ?

Ok Now try to understand differences between Sql interact with Sql Server and Linq interact with Sql Server.

Now, differences between Sql interact with Sql Server and Linq interact with Sql Server these are as follows:-

      Sql to Sql Sqrver                               Linq to Sql Server

1. Run time syntax checking of    1. Compile time syntax checking.

    Sql Server statement.
2. Not type safe.                            2. Type safe.
3. No intelligence support.            3. Intelligence support.   
4. Debugging of Sql statement      4. Debugging of Linq to Sql is 
    is not possible.                             available.
5. Code is combination of             5. It is pure Object Oriented.
    Object Oriented and relational
    database. 


Runtime syntax checking of sql server statement:
    Run-time syntax checking of sql server statement means we write the sql query under the double quotes in c#.net/vb.net then c# or vb compiler treat that query as a string. that's why C# or vb compiler do not compile that query at compile time. Because C# or VB compiler never compile the string.
When we write the query under the application within double quotes then every time the ADO.net carry the query and send to the database. After that Database Engine verify the syntax is define correct or not and the statement is defined perfectly or not. All the verification is done by the database engine not by the C# or vb compiler.
The main drawback is suppose if you are run the application in hundred machine at a time then every time and every machine send the statement to database and database engine verify and compile that statement so at that time extra burden on the server.

Compile time syntax checking:
  Compile time syntax checking means Linq queries is not verify by database engine. Because Linq queries is purely defined in our .Net languages. If you are working in C#.net then you write the Linq query in C# code and if you are working in VB.net then you write the Linq query in VB code. So,Internally their is a Linq query engine under our framework and this is a part of our framework.
And all the statement what ever you write in native language  compile by C# or VB compiler itself. C# or VB compiler only verifies weather the syntax is correctly define or not and also verify that statement is perfectly return or not.
The main advantage is statement is not compile each and every time because our application is compile one and only one time so, our statement is also compile one time and at the point of compile time verify the syntax and statement is perfectly defined or not. So, if you run your application in hundred machine then it is not required compile your program each and every time.

No type safe
  When we work with sql server with the help of ADO.net then the responsibility of developer remember everything about table that is a column name, data type and its size. Because here intelligence is not come that why the developer has remembered everything.
Suppose a developer develop an application and don't verify about data that means which type of data send in particular column, then at that time all the data goes to the database and after that database engine verify that data. Their after exception send to the application ,in this process only wastage of time. So, when working with sql server, developer responsible to verify everything about data.

Type safe
  When we work with Linq to Sql then no need to remember about tables column name and its data type because visual studio intelligence is appear particular table column name.

Debugging of Sql statement is not available
  In the application when we use SQL server query, then every statement write within the double quotes that's why our c#/vb compiler don't understand SQL statement and treat that statement as string. That's why in SQL statement in the application debugging is not possible. 

Debugging of Linq to Sql is available
 In the application when we use Linq to SQL then no need to write Linq query in double quotes. That's why our c#/vb compiler understand that statement and debugging in Linq to SQL is possible.

Code is combination of object Oriented and Relational.
 Code is a combination of Object-Oriented and relational such as suppose when we writing the insert/select/update/delete statement using sql under ado.net to interact with database means in the application we write the sql query inside the double quotes because our c#/vb compiler don't understand sql. So you responsible for pass the value which is came from either text box/checkbox/radiobutton/drop down etc... outside the double quotes. 
Ex:-  "insert into table_name values("+textbox1.text+", '"+textbox2.text+"')";

here in this example when we pass the textbox1 value in as a numeric value and textbox2 pass the string value that's why pass textbox2 value in single quotes.
Through this example we can see and understand code is combination of object oriented and relational under the sql to sql server. And it is work on the table, row, column, store-procedures etc...
 
It is pure Object Oriented.
  Linq to sql is pure object oriented because here no any relational code and no need to write the query in double quotes. suppose you write the code select statement in Linq to sql then write the codes directly because these codes are c#/vb statement only and these codes are compiled by the c#/vb compiler. The most important things is here when we are working with Linq to Sql we don't have tables at all, every tables become a class.

      Sql to Sql Server                               Linq to Sql
     ----------------------                         --------------------
        Tables                        =>                 Class
         - Columns                 =>                   -Property
         -Rows or records      =>                   -Instance
         - Stored procedures  =>                   - Methods
 In the Linq to sql every tables become to class. every columns goes to property, every row or records goes to Instance and every stored procedures goes to methods.


No comments:

Post a Comment