Menu Close

LINQ query syntax – Using C# LINQ

In this article we will learn about LINQ query syntax – Using C# LINQ – A Practical Overview. LINQ allows users to write SQL style of queries with in code using query syntax’s in C# or VB. Please read my previous article about Web API Routing.

.NET framework (3.5 or Higher) provides set of built in query keywords in LINQ to allow users to write SQL style of queries with in code.
There are two ways in which we can write queries in LINQ

  • Using Query Syntax
  • Using Method Syntax

LINQ Query Syntax

In LINQ. we write LINQ query by following certain rules regarding the syntax which is quite different from the SQL. We use below LINQ query syntax hierarchy to write a LINQ query.

from <variable> in <collection>
<where, joining, grouping, operators etc.> <lambda expression>
<select or groupBy operator> <format the results>

This order is to be followed while writing queries in LINQ. “from” keyword will form the starting point of the LINQ query followed by a user defined variable followed by “in” which actually specifies our source collection or Data source followed by a where clause, if there is a certain condition in the query can be used before the select to filter out the records and select is followed by group by and into clause.

LINQ Query Syntax Example

An example is shown below that uses LINQ. In this example we have an integer array having nine elements. We will print the elements from given array where element value greater than 5 and display them using LINQ.

These are the steps I follow to create the Console Application

  1. File New Project -> .Net Core Console application
  2. Specify the Project Folder path where you want to save your project.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 ,15, 34,22,45,65 };
            //LINQ Query Syntax to Print Numbers Greater than 5
            IEnumerable<int> result = from numbers in Num
                                      where numbers > 3
                                      select numbers;
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

When we run the application the output show like below:

LINQ Method Syntax

In LINQ, method syntaxes will use extension methods of Enumerable or Queryable static classes. Find the Difference between Difference Between IEnumerable, ICollection And IList Interface.

  •  IEnumerable collection extension methods Where and ToList() to query data based on our requirements. 

The Code description are like below.

using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9,15,67,45,3,45,8895 };

            //LINQ Method Syntax to Print Numbers Greater than 5

            IEnumerable<int> result = Num.Where(n => n > 5).ToList();

            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            
            Console.ReadLine();

        }
    }
}

When we run the application the output show like below:

Conclusion

In this article we will learnt about LINQ query syntax – Using C# LINQ – A Practical Overview.

Leave a Reply

Your email address will not be published.