Generation Operations in LINQ


Advertisements

A new sequence of values is created by generational operators.

Operator Description C# Query Expression Syntax VB Query Expression Syntax
DefaultIfEmpty When applied to an empty sequence, generate a default element within a sequence Not Applicable Not Applicable
Empty Returns an empty sequence of values and is the most simplest generational operator Not Applicable Not Applicable
Range Generates a collection having a sequence of integers or numbers Not Applicable Not Applicable
Repeat Generates a sequence containing repeated values of a specific length Not Applicable Not Applicable

Example of DefaultIfEmpty - Enumerable.DefaultIfEmpty.Method

C#

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

namespace Operators {
   class DefaultEmpty {
      static void Main(string[] args) {
      
         Pet barley = new Pet() { Name = "Barley", Age = 4 };
         Pet boots = new Pet() { Name = "Boots", Age = 1 };
         Pet whiskers = new Pet() { Name = "Whiskers", Age = 6 };
         Pet bluemoon = new Pet() { Name = "Blue Moon", Age = 9 };
         Pet daisy = new Pet() { Name = "Daisy", Age = 3 };

         List<Pet> pets = new List<Pet>() { barley, boots, whiskers, bluemoon, daisy };

         foreach (var e in pets.DefaultIfEmpty()) {
            Console.WriteLine("Name = {0} ", e.Name);
         }

         Console.WriteLine("\nPress any key to continue.");
         Console.ReadKey();
      }

      class Pet {
         public string Name { get; set; }
         public int Age { get; set; }
      }
   }
}

VB

Module Module1

   Sub Main()

      Dim barley As New Pet With {.Name = "Barley", .Age = 4}
      Dim boots As New Pet With {.Name = "Boots", .Age = 1}
      Dim whiskers As New Pet With {.Name = "Whiskers", .Age = 6}
      Dim bluemoon As New Pet With {.Name = "Blue Moon", .Age = 9}
      Dim daisy As New Pet With {.Name = "Daisy", .Age = 3}

      Dim pets As New System.Collections.Generic.List(Of Pet)(New Pet() {barley, boots, whiskers, bluemoon, daisy})

      For Each e In pets.DefaultIfEmpty()
         Console.WriteLine("Name = {0}", e.Name)
      Next

      Console.WriteLine(vbLf & "Press any key to continue.")
      Console.ReadKey()
   End Sub

   Class Pet
      Public Property Name As String
      Public Property Age As Integer
   End Class
   
End Module

When the above code of C# or VB is compiled and executed, it produces the following result −

Name = Barley 
Name = Boots 
Name = Whiskers
Name = Blue Moon
Name = Daisy

Press any key to continue.

Example of Range - Enumerable.Range Method

C#

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

namespace Operators {
   class Program {
      static void Main(string[] args) {
         // Generate a sequence of integers from 1 to 5  
         // and then select their squares.
		 
         IEnumerable<int> squares = Enumerable.Range(1, 5).Select(x => x * x);

         foreach (int num in squares) {
            Console.WriteLine(num);
         }
			
         Console.ReadLine();
      }
   }
}

VB

Module Module1

   Sub Main()
   
      Dim squares As IEnumerable(Of Integer) = _Enumerable.Range(1, 5).Select(Function(x) x * x)

      Dim output As New System.Text.StringBuilder
	  
      For Each num As Integer In squares
         output.AppendLine(num)
         Console.WriteLine(num)
      Next
		
      Console.ReadLine()
	  
   End Sub
   
End Module

When the above code of C# or VB is compiled and executed, it produces the following result −

1
4
9
16
25

Example of Repeat - Enumerable.Repeat(Of TResult) Method

C#

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

namespace Operators {
   class Program {
      static void Main(string[] args) {
         IEnumerable<string> strings = Enumerable.Repeat("I like programming.", 3);

         foreach (String str in strings) {
            Console.WriteLine(str);
         }
			
         Console.ReadLine();
      }
   }
}

VB

Module Module1

   Sub Main()
   
      Dim sentences As IEnumerable(Of String) = _Enumerable.Repeat("I like programming.", 3)

      Dim output As New System.Text.StringBuilder
	  
      For Each sentence As String In sentences
         output.AppendLine(sentence)
         Console.WriteLine(sentence)
      Next

      Console.ReadLine()
	  
   End Sub
   
End Module

When the above code of C# or VB is compiled and executed, it produces the following result −

I like programming.
I like programming.
I like programming.
linq_query_operators.htm
Advertisements