Generation Operators in LINQ

by Sachin Singh


Posted on Saturday, 06 February 2021

Tags: Generation Operators in LINQ

In LINQ, as the name suggests ,generation operators are used to create or generate a new sequence of elements. In LINQ we have a different type of generation operators available those are
    • DefaultIfEmpty
    • Range
    • Repeat
    • Empty
   

These LINQ generation operators will help us to generate a new sequence of elements. The following diagram shows more detailed information related to generation operators in LINQ.

Linq architecture
Linq architecture

As the name suggests , if the collection is null then it will return a default value for example if a collection of string is null it will return null whereas if the collection of integers is null then it will return zero. Not only this, with DefaultIfEmpty we can specify a default value explicitely, Now the question arises , when do we use it? we use it to avoid nulls. That is why it is used to achieve left joins in LINQ because if a collection will be joined with a default collection then it will return the items of first collection.

Example: Return default value if EmployeeList is null.

       public static void Main()
        {
            List<Employee> employees = new List<Employee>();       
     foreach (var item in employees.DefaultIfEmpty(new Employee() 
       {Id=0,Name="",Gender="",MaritalStatus="",Phone=0,Salary=0}))
            {
                Console.WriteLine(item.Name);
            }
                Console.ReadLine();
        }

Example: Return 100 as default value if List of Integers is null.

     public static void Main()
        {         
            var numbers = new List<int>(100);
            foreach (var num in numbers)
            {
                Console.WriteLine(num);         
            }
          Console.ReadLine();
        }

Empty()

Whenever we try to iterate over a collection and access their properties then we get null reference exception if the collection is null , to avoid null reference exception we can send an Empty collection if the collection is null. Empty Operator is used to return an Empty collection of specified type. So, whenever you have to return IEnumerable<T> then you can return an Empty collection in else part so that the caller will not get any null reference exception while iterate over the collection.

Example:

      public static void Main()
        {
            foreach (var employee in Get())
            {
                Console.WriteLine(employee.Name);      
            }
           
               Console.ReadLine();
        }

        public static IEnumerable Get()
        {
            List<Employee> empList = new List<Employee>();
            if (empList!=null)
            {
                return empList;
            }
            return Enumerable.Empty<Employee>();
        }

Range Operator

Range Operator is used to return a sequence of integers in a specified range, for example, with Range operator, you can return 10 integers starting from 20 as shown below.

Example:

      public static void Main()
       {
            var range = Enumerable.Range(20, 10);
            foreach(var num in range)
            {
                Console.WriteLine(num);
            }
                Console.ReadLine();
        }

Repeat

The Repeat Operator is used to return a collection of specific number of same item

Example: print "sharpencode.com" 10 times

        public static void Main()
        {
            var repeatedItems = Enumerable.Repeat<string>("sharpencode.com",10);
            foreach (var item in repeatedItems)
            {
                Console.WriteLine(item);
            }
                Console.ReadLine();
        }

In the coming chapters, we will discuss all these LINQ generation operations in detail with examples.