C#

Restriction Operator in LINQ



In this article, we are going to learn LINQ restriction operator  in detail  with the examples. The Where standard query operator belong to restriction operators category. Where query operator is same as that of Where clause in SQL. Where query operator is used to filter the rows.

The where clause is not a mandatory clause of LINQ statements, but can be used to limit the number of records affected by a LINQ, the where clause can be used to extract records from select, delete, update and so on.

The filter expression is specified using predicate. predicate is a type safe function pointer or we can say predicate is a delegate used to test each element based on a specific  condition.
Let us understand restriction operator with an example.

Example 1:-
For example , you have a integer numbers array and you want to return the numbers which is divided by 3. For that you can use where operator in our linq query.

In this example i am taking an array of integer. Please see below image for more detail.

restriction operator


For practical you can copy and paste below code in visual studio console application.

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

namespace Practice
{
    class Program
    {
        public static void Main()
        {
                        //numbers list
            int[]num={ 1, 2, 3, 9, 12, 13, 14, 15, 18, 21, 22, 23, 24, 25, 26, 27 };
            IEnumerable<int> result = num.Where(x => x % 3 == 0);//where operator
            //numbers which is divide by 3 are
            foreach(int n in result)
            {
                Console.WriteLine(n);
            }
            Console.ReadLine();
        }
    }
}

The output is

restriction operator


Note:- Where operator in optional in LINQ query

We can also write linq query in T-SQL expression

restirction operator


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

namespace Practice
{
    class Program
    {
        public static void Main()
        {
            int[]num={ 1, 2, 3, 9, 12, 13, 14, 15, 18, 21, 22, 23, 24, 25, 26, 27 };
            //IEnumerable<int> result = num.Where(x => x % 3 == 0);
            //the linq query with T-SQL expression
            IEnumerable<int> result = from digits in num
                                      where digits % 3 == 0
                                      select digits;
            //numbers which is divide by 3 are
            foreach (int n in result)
            {
                Console.WriteLine(n);
            }
            Console.ReadLine();
        }
    }
}

The above example we can re-write with below code

restirction operator


namespace Practice
{
    class Program
    {
        public static void Main()
        {
            int[]num={ 1, 2, 3, 9, 12, 13, 14, 15, 18, 21, 22, 23, 24, 25, 26, 27 };

            Func<int, bool> predicate = x => x % 3 == 0;

            IEnumerable<int> result = num.Where(predicate);
           
            //numbers which is divide by 3 are
            foreach (int n in result)
            {
                Console.WriteLine(n);
            }
            Console.ReadLine();
        }
    }
}
In where clause we are passing Func<int, bool> predicate expression which is a delegate or type safe function pointer. This predicate expects an int input parameter and returns a Boolean value.

Example 2:-

In this example we get index position also.
using System;
using System.Collections.Generic;
using System.Linq;

namespace Practice
{
    class Program
    {
        public static void Main()
        {
            int[]num={ 1, 2, 3, 9, 12, 13, 14, 15, 18, 21, 22, 23, 24, 25, 26, 27 };
            var result = num.Where(x=>x%3==0).Select((n,i)=>new { Number=n,Index=i});
            //numbers which is divide by 3 are
            foreach (var item in result)
            {
                Console.WriteLine("Number- "+item.Number+" , "+" Index - "+item.Index);
            }
            Console.ReadLine();
        }
    }
}

The output is

restirction operator



And If you want to return only Index then

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

namespace Practice
{
    class Program
    {
        public static void Main()
        {
            int[]num={ 1, 2, 3, 9, 12, 13, 14, 15, 18, 21, 22, 23, 24, 25, 26, 27 };
            var result = num
                .Select((n, i) => new { Number = n, Index = i })
                .Where(x => x.Number % 3 == 0)
                .Select(x => x.Index);
            //numbers which is divide by 3 are
            foreach (var item in result)
            {
                Console.WriteLine("Index - "+item);
            }
            Console.ReadLine();
        }
    }
}

The output is

restirction operator


For real time example you can get only male employee from db using restriction operator.







No comments:

Post a Comment