Quote for the Week

"Learn to enjoy every moment of your life"

Thursday, July 24, 2014

LINQ Article Posted by Sumanth Bodhuluri

LINQ  :
---------                   

Basically LINQ address the current database development model in the context of Object Oriented Programming Model.

What exactly is Linq ?

For a beginner these are common question, Now we will discuss about these questions clearly .Before we do anything let us understand what is linq?

We know that whenever retrieve a data from the database we write select statement.         

 SELECT STATEMENT :

Where clauses, orderby clauses, groupby clauses, having all these are part of  select statement.As a developer of languages like C# & VB.NET would have to learn sql commands to communicate with the databases for operationsThe  problem here is the developer must learn sql commands properly , and when he is writing the sql statements in frontend  will be  purely in string format  that does not have typechecking and no validations of syntax in the frontend . The  result will be displayed when the sql statements sends to the backend then it should be verified the syntax .If the syntax is right it executes the results i.e., which will fetch some object from the dataset or  dataReaderThe point here we must know SQL statements,  properly frame them and then submitted to the backend likewise we got another types of collections like Arrays, Lists & Dictionaries those are also used for getting the data from the dataSource .We have different sources of data like Xml documents , relational data sources and objects but here  the issue is we have to retrieve the data from the sources. The requirement is same i.e., retrieve the data.


here we requires different API’s to fetch data from different data sources “.

Let us discuss with some different scenarios here:


sc1:  Like if I want to fetch data from a list I can only use methods of class list.

sc2:  If I want to operate on stack, queue, linkedlist. All these   I am restricted to the classes alone.

sc3:  Likewise if I am using ADO.NET for fetching the data from the database and I again get restricted to only those functionalities which are available in ADO.NET API. Moreover it is completely different API.

           sc4:   I may use dataReader or dataset likewise we must have coustom objects we have to fetch data and again a different API for that.

          sc5:  We might have xml has source of data there also we request different API  Now a question raised that Why to have different API for different sources of data? When probably operation we did on that data source be almost similar ?”

THEN THE ANSWER  IS....

LINQ:

I hope with the above information we can understand the complexities faced in .net framework beforeNow we can go through, What exactly is Linq?Linq is a querying language feature and has a great power of querying on any source of data .What It mean is we are going to query in a programming language (C#, VB.NET and other .NET Compatible languages) and submitted to data source for retrieving data to us. So, that is what the LINQ facilitates is……Here Data source could be the collection of objects, database (or) XML files (or) oracle database (or) sql sever database (or) it can be any other dataSource which are implemented IEnumerable<T> interface. Of course it is a generic version  Microsoft basically divides LINQ into three areas:


1.    Linq to Object
2.    Linq to ADO.NET
3.    Linq to Xml (Xlinq)

LINQ TO OBJECTS :

What exactly I mean Linq to objects is we are going to perform queries against objects which are in memory. We have some objects created in memory those objects are going to perform our query.The objects can:  Collection objects, Arrays objects which are costumed user defined object from there we have to fetch the data .

For Example:          Collection of employee objects in memory.



We would like to perform some query on those employee objects.Like we need:     



 All the employees sorted by their names. All the employees sorted by their salary. To perform filter condition. Group all the employees by department nameIn these kind of cases we can use the linq to object concept.Linq queries gives us ability to perform operation on those objects which are actually not present in the methods in the corresponding class. That’s the beautiful power to see in programming.



LINQ TO ADO.NET:

          Linq to ADO.NET is further categorized into three:

 1) Linq to SQL:
                             Where we are perform query a sql sever database.     In short only on sql sever database not anything else.Now the point is now we will write query in C#. How is that going to execute against a sql server DataBase.This is the reason why the Linq Providers come into picture.


2)Linq to DataSet: 

                              Now we discuss about the how the Linq queries perform on the DataSet.We might had DataSet created.

 We can create DataSet which read on :
  1) DataAdaptor                                     
  2) Reading data from xml document

                            We might have got the dataset constructing dynamically by creating on datable and adding to the table whatever and adding to the table  collections and adding rows and columns to the data table.Whatever the way you created dataset from the dataset you have to perform query operations.

YES! Again linq can be used:

3) Linq to Entities :

Linq to entities is the solution provided for querying data against the entities which are created using entity Framework.What is entity Framework? Entity framework is an ORM solution provided by Microsoft.ORM: Object Relational Management solution.So, we are going to have some type of datasource through which will be created in memory and from those objects again we can fetch data using linq.So, Linq to entity is a purely for Entity Framework. 

LINQ to XML (XLINQ):

Linq to xml formally called as XLINQ.Here we perform query against any kind of  XML document.You might have an XML document which is structured XML data will be there in that, from there we can fetch the data.In this scenario we will use Linq to XML.Here in XLINQ: X------à Extending.

Note: Do you Have an Article about .Net, mail to: dotnetcircle@gmail.com


No comments: