Different LINQ (Language Integrated Query) Technologies

In this article, you will understand different LINQ technologies and query expressions with different standard query operators.

1] LINQ to objects
2] LINQ to XML
3] LINQ to SQL
4] LINQ to entites

LINQ to Objects

So, we can query in-memory objects. In this feature of LINQ, There’s a lot of Foreach loops that you can replace with a declarative LINQ query and it saves a lot of code. a lot of places where you iterate through things.

The LINQ to Objects refers to the use of LINQ queries with any IEnumerable or IEnumerable generic collection directly. without, the use of an intermediate LINQ provider or API such as LINQ to XML LINQ to SQL. we can use LINQ to query any enumerable collections such as Array, List, Dictionary. The collection will be user-defined or it will return by a .NET Framework API. The LINQ to Objects represents a new approach to collections. In the old way, we write complex Foreach loops that specified how to retrieve data from a collection. In the LINQ approach, you write declarative code that describes what you want to retrieve using different filtering, projection and grouping operators.

LINQ queries are more concise and readable, especially when filtering multiple conditions. and provide powerful filtering, ordering, and grouping capabilities with a minimum of application code.

Syntax:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqExample
{
  class Program	
  {
   IEnumerable<Items> Itemslist = new List<Items>()
   {
     new Items {ID=1, Name="John", Price=1000},
	 new Items {ID=2, Name="Michel", Price=2000},
	 new Items {ID=3, Name="Robin", Price=3000},
	 new Items {ID=4, Name="Justin", Price=4000}
   };
	IEnumerable<Items> query = from i in Itemslist where
	                           i.Price < 3000 orderby 
				   i.Name select i;
	}
}

LINQ to XML

Another LINQ technology that shifts with the .NET framework is LINQ to XML. And it turns out that LINQ to XML is more than querying XML. There’s an entirely new XML API and it’s in the System.Xml.Linq namespace. the XML API that is in the System.XML and classes like XMLDocument and XMLElement.

linq to xml

That API is a very formal API And when you’re doing XML intensive work with that API you tend to write a lot of helper methods to see you can avoid writing lines and lines of code and try to keep things readable. This API– First of all, it’s very familiar. So instead of the XML document, you have the class called “The XDocument”. And instead of the XML element, there’s a class called the “XElement” and there are XAtrribute and XComment and XNamespace, and all the types that you would expect to find in an XML API. But it’s designed to be lightweight and more , it’s designed to be program or friendly.

syntax:

XElement ItemsList = new XElement("Items",
new XElement("Item","Pen"),
new XElement("Item","Mobile"),
new XElement("Item","Table"),
new XElement("Item","Laptop"),
);

ItemsList.toString();

It will generated like this.

<Items>
<Item>Pen</Item>
<Item>Mobile</Item>
<Item>Table</Item>
<Item>Laptop</Item>
</Items> 

LINQ to SQL

The LINQ to SQL is a technology that would fall into the category of being an object-relational mapper. And there are several of these out there. That is an object-relational mapper you point it to a relational database. you give it some meta information, some mapping information, how to– that is how to take results that are coming back from the database and transform those results into objects. And then you can use the object-relational mapper to query the database and update the database. So, with LINQ to SQL, we’ll be writing LINQ queries that get translated into Transact SQL to bring back objects. And then we can set properties on those objects, give them different values to each property and then tell LINQ to SQL to update the database. And then we’ll figure out the SQL commands necessary to change the related records back in the database.

Now, LINQ to SQL only works with SQL Server. It does work with 2000, 2005 and 2008, and also the compact edition of SQL Server. But it does not work with Oracle or DB2 and those other databases. That’s going to be the job of the entity framework. When we have a LINQ query like the one we have on the screen, it’s LINQ’s job to take that query and transform it into a structured query language. So there’s a LINQ provider behind this. The provider understands how to generate SQL for us. we want to get those list of records from context. So, context is an instance of a data context class which is a special class in LINQ to SQL. So the properties on the data context class represent tables in your database.

Syntax: Translate query expression into T-SQL

IEnumerable<Item> Items = from itm in context.Items where
	itm.Name == "Laptop" orderby itm.ItemId acending select itm;
	
SELECT [t0].[ItemId],[t0].[Name],[t0].[Height],[t0].[Width] from [dbo].[Items] As [t0] where [t0].[Name] = @val ORDER BY [t0].[ItemId]

LINQ to Entity

Finally, let’s understands the Entity Framework. Now the Entity Framework and LINQ to SQL are similar technologies. they are both object-relational mappers. But, the Entity Framework, to be supported by a larger development team. It does have some more features. In some ways, it is very different from LINQ to SQL.

The Entity Framework will allow you to query and update the database objects. So, there’s a class very similar to the DataContext that retrieves information from the database. It gives you objects, you can set properties on those objects and tell this class to persist those changes into the database. So, there is LINQ to Entities, the Entity Framework likes to define the classes that we’re creating as entities. LINQ to Entities will be transformed into SQL Commands for SQL Server. Entity Framework was also designed to support additional databases. Entity Framework also supports another option for querying the database which is a language called Entity SQL which looks very much like a structured query language. but, it’s geared towards working with entities, that is working with objects instead of working with rows of data in a database.

Syntax:

using(SampleDbEntities db = new SampleDbEntities())
{
     var employees = from e in db.Employees
     where e.FirstName == "John"
     select e;
}

Leave a Reply

Your email address will not be published. Required fields are marked *