8 Compelling Reasons to Use LINQ Queries

 

Language-Integrated Query (LINQ) is a query language that can be used to query a number of data sources. LINQ is built into .NET Languages such as Visual C# and has standardized, declarative query syntax. You can use LINQ to query data sources such as Collections, ADO.NET dataset, SQL Server databases and XML documents; that is any data source that implements the IEnumerable interface.

Using your knowledge of C# and a few keywords and concepts you don’t have to learn an extra language such as SQL or XQuery to query data sources, you can use the following basic syntax in your C# code when querying data sources with LINQ:

from <variable names> in <data source>

group <grouping criteria>

where <selection criteria>

orderby <result ordering criteria>

select <variable name>

Apart from having a standardized syntax for querying data sources LINQ has many other benefits that need to be mentioned. A huge help when writing the code is compile-time checking of type-safety and syntax errors, and to help you on the way IntelliSense is available. Another huge thing is that you use the same familiar syntax regardless of what data source you are targeting. When querying you use a consistent model with powerful filtering, ordering and grouping capabilities that can be likened those in SQL. When querying using LINQ the data is fetched when the query is executed, this means that you can build on an existing query without fetching the data directly. A very powerful feature of LINQ is the possibility to transform the fetched data without changing the underlying data model.

When working with XML, LINQ lets you do that with the XML elements directly without first having to create a XML Document. LINQ handles the XML in-memory in a powerful way, making it easier to use than XPath and XQuery.

LINQ queries are based on generic types, so a basic knowledge of generic types is beneficial when using LINQ. Generics are most frequently used with List and Dictionary collections that store objects of type T, where T is substituted for the desired type. This is a very powerful alternative to a non-type safe ArrayList collection that stores any type of objects. The List<T> class provides a way to create type-safe strongly typed collections.

The following example shows how to create a type-safe collection for storing book objects, created with the generic List<T> collection.

List<Book> books = new List<Book>();

When selecting data, the minimum syntax that you need is from … in … select this will give you the all the data from the data source. Often when selecting data you do not want to execute the LINQ statement at once but rather manipulate it further before executing it. To do this, you store the fetched data in a variable of type IQueryable<T>. If you want to force the execution of the LINQ statement, you can call one of the following methods ToList, ToArray, or ToDictionary.

The following example shows how to create an IQueryable<T> variable to hold a LINQ query without executing it.

IQueryable<Book> books;

When fetching data, you sometimes need to transform the result to fit the data model you are using. Imagine that you want to display certain information to the user, but the information is stored in different tables in the database; one way to solve this dilemma is to create a new type that will contain consolidated data these tables. You can either create a new class that defines the type you want to use, or you can create an anonymous object.

The following example shows how you can consolidate data from the Student table and its related Teacher table using a LINQ query.  The new keyword is used to create an anonymous object containing data from the two tables. Because the First method is called the query will be executed immediately and only returns the first student; it also means that the variable will contain an instance of the anonymous object that not is of an IQueryable type.

var studentInfo = (

    from student in DBContext.Student

    select new

    {

        Teacher = student.Teacher.FirstName + " " +

        student.Teacher.LastName,

        Student = student.FirstName + " " +

        student.LastName,

    }).First();

 

Conclusion, LINQ queries have several benefits such as:

  • A standardized syntax for querying data sources.
  • compile-time checking of type-safety and syntax errors
  • IntelliSense is available when writing queries.
  • You can build on an existing query without fetching the data directly.
  • Possibility to transform fetched data without changing the underlying data model.
  • Anonymous objects can be used when transforming data.
  • You can work with XML elements directly without having to create a XML Document.
  • Easier to use than XPath and XQuery when querying XML.

 

Stay connected with news and updates!

Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.

Subscribe
Close

50% Complete

Two Step

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.