Microsoft .NET Framework (4 Blogs) Become a Certified Professional

Unleash the Power of LINQ, the .NET Way

Last updated on Oct 28,2020 12.6K Views

Divyamol
Divyamol is passionate about learning new technologies and exploring things around her.... Divyamol is passionate about learning new technologies and exploring things around her. She is currently working in Edureka as a Quality Analyst.

LINQ that stands for Language Integrated Query (pronounced as “link”) is a .NET language extension that supports data retrieval from different data sources like XML document, databases and collections. It was introduced in the .NET 3.5 framework. VB and C# are the languages that have LINQ capabilities.

The History:
One would think why LINQ was developed when ADO.NET was there for data retrieval. The main reason behind this was due to the concept “how the application saw relational data.” If we are working with a simple application that connects to a simple database, everything works well with ADO.NET. For example, a manufacturer’s ordering system in which there is a table for a customer and an order table that stores information regarding the order he makes.

However, if the database gets bigger with more tables i.e. the order table gets bigger with multiple tables for storing related data and also, when the application gets complex, we see an increasing amount of difference in the way application looks at the data.
But still the programmer would want to work a single conceptual order without having to do a complex join operation for every order-related query. This is called the object-relational impedance mismatch.

For this reason, programmers implement their mapping layers to create a single entity like an object. For example, order object has methods for retrieval and update, which perform the database queries to get information.

Mapping layers, in short, conveniently isolate the application from the specifics of the logical database structures. In LINQ, there is a mapping between the data schemas in the object as well as the relational domain which makes it simpler to use.

A Real-Time Use Case:
LINQ is used in many applications for various needs, but one of the real time examples that I can mention is how LINQ was used to build LINQ to Twitter. LINQ to Twitter is an open-source third party LINQ Provider for theTwittermicro-blogging service. It uses standard LINQ syntax for queries and includes method calls for changes via theTwitter API.

LINQ Architecture:

Users can query XML documents, relational databases and in-memory collections by querying using LINQ. The languages that support LINQ capabilities are VB , C#, etc. The data sources are connected to the language by the LINQ-enabled data sources which includes the LINQ flavors like LINQ to Objects, LINQ to ADO.NETand LINQ to XML.

LINQ Flavors:
The main flavors of LINQ are:

  • LINQ to objects – Allows querying in-memory objects like arrays, lists, generic list and any type of collections.
  • LINQ to XML – Allows querying the XML document by converting the document into XElement objects and then querying using the local execution engine.
  • LINQ to ADO.NET – It includes:
  1. LINQ to SQL- It’s specifically used to work with the SQL server database.
  2. LINQ to dataset – Allows query to any database that can be queried with ADO.NET.
  3. LINQ to entities – It is similar to LINQ to SQL. It allows developers to query the conceptual entity data model
  • Parallel LINQ – this is an extension of the LINQ to objects that has a parallel programming library. Using this we can split the query to execute simultaneously on different processors.

How does it benefit the developer?

  • You do not need to learn a new language as it is similar to SQL. LINQ is simple and also neat.
  • LINQ is type safe, so query errors are type checked at compile time rather than the old way of finding mistakes in query during the run-time. In short, it makes the process of debugging faster.
  • Reduces the amount of code you have to write, which gives you better performance and positions you for other technologies (like Parallel LINQ, or PLINQ, parallel processing).
  • It provides IntelliSense support for your range variable as you type in the rest of your LINQ statement.
  • By using a LINQ query, you can use a source sequence as input and modify it in many ways to create a new output sequence (data transformation). You can modify the sequence itself without modifying the elements themselves by sorting and grouping.
  • If your application had sub-query to be used, using SQL queries gets tougher and the query gets longer, but by using LINQ it simplifies the same.

A Performance Booster:
LINQ is more productive and enhances the performance of the program due to the following reasons:

Using LINQ you can break down a query into parts, and then re-use some of those parts across your application.

  • You can also shift part of your query to a less loaded server for processing i.e. reordering, transforming and regrouping results; thus reducing the burden on the server.
  • LINQ lets you retrieve shaped or hierarchical data. This avoids duplication, makes results easier to work with, and in most cases it even removes the need for using joins.
  • While building an n-tier architecture application, using LINQ-enabled data-access layer (using an API such as LINQ to SQL or Entity Framework), cuts the data access development time by more than a half, as well as making maintenance far easier

LINQ in .NET – A Family Matter:
LINQ query structure:

The LINQ Query contains combination of clauses which specify data sources and iteration variable for the query. The query expression may include Filtering, Grouping, Sorting, Joining, Calculating etc.

There are three parts in a LINQ Query operation:

  • Obtain the data source: Specifies an array or collection or database or XML
  • Create a query: Using var keyword
  • Execute the query: Retrieve or fetch using loop

Example: 1
To display the items in an array:

static void Main()
 {
// The Three Parts of a LINQ Query: 
// 1. Data source. 
int[] numbers = newint[7] { 0, 1, 2, 3, 4, 5, 6 };

// 2. Query creation. 

var numQuery =
from num in numbers select num;

// 3. Query execution. 
foreach (int num in numQuery)
 {
Console.Write("{0} " + num);
 }

Console.ReadKey();

Example: 2
To display the numbers divisible by 2 (Using Filtering)

static void Main()
 {
// The Three Parts of a LINQ Query: 
// 1. Data source. 
int[] numbers = newint[7] { 0, 1, 2, 3, 4, 5, 6 };

// 2. Query creation. 
// numQuery is an IEnumerable<int> 
var numQuery =
from num in numbers
where (num % 2) == 0
select num;

// 3. Query execution. 
foreach (int num in numQuery)
 {
Console.Write("{0} " + num);
 }

Console.ReadKey();
 }

C# Features That Support LINQ:

  • Query Expression: It is a query syntax that is used to retrieve data from LINQ-enabled data source.
  • Implicitly typed variables: A variable var, which enables the compiler to infer the type of the variable.
  • Object and collection initializers: Enables the initialization of objects without explicitly calling a constructor for the object.
  • Anonymous types: Enables the compiler to create objects without requiring that you specify a named data type. The type name is only available to the compiler.
  • Extension methods: Enables the extension of any existing type by associating static methods to the type.
  • Lambda expressions: An inline expression or statement block that can be used wherever a delegate type is expected.

C# features that support LINQ

Is LINQ a Reason Enough to Choose .NET?
LINQ is a language that has proved to be beneficial to the developers as it is similar to SQL and C# and VB languages, we normally use in the .NET framework. The main benefit of using the language is because of the performance boost it gives. It also reduces the impedance mismatch problem; i.e. the gap between the representation of the data from storage site to the objects you use in your applications.

So if you want to explore and take advantage of the best in class data-retrieval mechanism, the power it gives to the developers and the ease of use, then LINQ is the way to go.

Got a question for us? Mention them in the comments section and we will get back to you.

Related Posts:

Get Started with Microsoft .NET

Microsoft .NET Framework: An Intellisense way of web development

Comments
2 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Unleash the Power of LINQ, the .NET Way

edureka.co