What is LINQ (Language Integrated Query)

In this article, we are going to understand the overview of
Language Integrated Query. Specifically, we’re going to learn why do we have LINQ and Query Expression. we’re going to look at our first couple of LINQ queries with major LINQ technologies. like LINQ to Object, XML, SQL, Entites for in memory database or entity data models.

Introduction to LINQ

When C# was still a young language and the language designers had the dream of adding a query syntax feature to the C# language. At that time, where our data lived determined the API and the tools that we would use to access the data. For example, if our data was in memory, a collection of objects, you’d use an API provided by the generic collection classes in. NET to access the data. If our data was in SQL Server and we need to use ADO. NET to send SQL commands to the database or XML data, we had a complete set of XML APIs to query XML documents.

The LINQ query gives us strongly type compile-time checks on queries. that can execute against in-memory data, XML data and relational data. Once you learn the basic Language Integrated Query operators and syntax. The LINQ query use to a wide variety of data sources. because, there have been providers built that allow us to use LINQ to query document databases, JSON data, the file system, and much more. So, let’s take a look at how LINQ can improve our C# code by solving a specific problem.

Standard Query Operators

The LINQ API consists of what we call the stand query operators and there are over 50 operators available. So the operators are defined in the System.Linq namespace. they work on any object that implements IEnumerable of any type. The .Net supports generics. so these operators work in c#, visual basic, F#. and they working in different languages

Standard Query Operators

Now, Let’s understand our first query. here we have the string an array of animals names and a string array implements IEnumerable of T.

string[] animals = { "tiger", "horse", "dog", "cat", "rat", "deer"};

IEnumerable<string> query = from a in animals where a.Length == 5
orderby a descending select a;

foreach(string ani in query)
{
 console.writeline(ani);
} 

We select each string that we find after they’ve been filtered and ordered. the Where, OrderBy and select operators are the standards operators. It is a structured query language.

So, we will implements query syntax in the class. let’s create Items class and some few properties.

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

namespace LinqExample
{
  public class Items	
  {
   public int ID { get; set;}
   public int Name { get; set;}
   public int Price { get; set;}

   }
}

Now, we’ll switch back to our program. the first thing we’ll have to do is create a list of students and some sort of data source that we can actually query. so here we have list of records.

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}
   };
   }
}

So, what we’re going to get back from queries is as a new IEnumerable of Items list, and we’ll start writing this as IEnumerable and range operator.

We want to filter those Items who have a price less than 3000 here we’re doing project We’d be selecting the name of each Items.

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;
	}
}

After the query has been defined, we have some results if our for each is right.

Output:-
John
Michel

LINQ Query Expression

The same standard query operators work everywhere

1] Relational data
2] Objects
3] XML data

Leave a Reply

Your email address will not be published.