Introduction To LINQ


In this article we will learn LINQ. The article explains what LINQ is, why to use LINQ and how to use LINQ. When there was already ADO.NET technology with .NET then why another data access technology? What are the problems solved using LINQ? These are all the things in this article.

The following are the topics covered:

  • What LINQ is 
  • How to write a LINQ Query

What LINQ is

Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, including Java, PHP, JavaScript and ActionScript. Let's try to understand what exactly "Language Integrated" means. LINQ is part of programming language syntax. And query means LINQ is used for querying the data. The data would be any type, relational type data and objects too. So we can say that a LINQ is a Programming language syntax used to query data.

Let's try to understand what ADO.NET is then why LINQ. ADO.NET is an object-oriented library and uses data from a relational perspective. We can bind ADO.NET objects directly into a user interface. But we often need a translation, like ADO.NET data into Business Objects with relationships, rules and semantics that do not translate automatically from a relational store. LINQ does remove these kinds of problems and re-shape our data as an object. 

LINQ also defines a set of methods names called standard query operators, or standard sequence operators along with translation rules used by the compiler to translate a fluent style of query expressions using these methods names, lambda expressions and anonymous types.        

How to write LINQ Query

Before writing a LINQ query we should understand the basic syntax of LINQ. There are the following three ways to write a LINQ query.

  • Query Syntax
  • Method Syntax
  • Combination of query syntax and method syntax  

Query Syntax

This is the basic and simplest way to write a query using query syntax. In query syntax we must use a query expression. In a query expression the first expression demonstrates how to filter records by applying a where clause, this will return all the elements determined by the condition passed in the where clause.


  1. List<int> marksOfStudent = new List<int>() { 98,45,32,88,59,76,69,90,89,73,66,12,30,26,55};  
  2.           IEnumerable<int> query1 =  
  3.             from marks in marksOfStudent  
  4.             where marks < 30  
  5.             select marks;  
  7.         IEnumerable<int> query2 =  
  8.             from marks in marksOfStudent  
  9.             where marks < 30 || num > 65  
  10.             orderby num ascending  
  11.             select num;  
  13.         string[] StudentName = { "Rajeev""Ranjan""Prerana""Nisa""Rohit" };  
  14.         IEnumerable<IGrouping<charstring>> student =  
  15.             from item in StudentName  
  16.             group item by item[0];  

Method Syntax

When a query is written with a method call and returns a single numeric value, like Max, SUM, Min and many others. These methods must always be called last of any query because they represent only by a single value and cannot serve as the source for an additional query.


  1. List<int> studentClass1 = new List<int>() { 58, 94, 71, 55, 92, 89, 76, 87, 12, 30 };  
  2. List<int> StudentClass2 = new List<int>() { 25, 54, 87, 67, 90, 66, 77, 44, 92, 23 };  
  4. double average = studentClass1.Average();  
  6. IEnumerable<int> concatenationQuery = studentClass1.Concat(studentClass2);  
We can also write these queries as in the following:
  1. var average = numbers1.Average();  
  2. var concatenationQuery = numbers1.Concat(numbers2);  

Combination of query syntax and method syntax

Mixed Query and Method Syntax will help to use a single query with a method as well as a simple query.


  1. int marks = (from num in studentClass1  
  2.                       where num < 33 || num > 65  
  3.                       select num).Count();  
  5.         IEnumerable<int> marks = from num in studentClass1  
  6.                                  where num < 33 || num > 65  
  7.                                  select num;  
  9. int num2 = numbersQuery.Count();  


In this article we learned the basics of LINQ and the syntax of LINQ. Thanks for reading this article.


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now