In LINQ, as the name suggests, the Generation Operators are used to create or generate a new sequence of elements. In LINQ, we have different types of generation operators available:
- DefaultIfEmpty
- Range
- Repeat
- Empty
These LINQ generation operators will help us generate a new sequence of elements. The following diagram shows more detailed information related to generation operators in LINQ:
As the name suggests, if the collection is null, it will return a default value. For example, if a collection of strings is null, it will return null, whereas if the collection of integers is null, it will return zero. Not only this, but with DefaultIfEmpty, we can specify a default value explicitly. 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 is joined with a default collection, it will return the items of the 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 the default value if the 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 its properties, we get a null reference exception if the collection is null. To avoid a null reference exception, we can send an empty collection if the collection is null. The Empty Operator is used to return an empty collection of the specified type. So, whenever you have to return IEnumerable<T>, you can return an Empty<T> collection in the else part so that the caller will not get any null reference exception while iterating over the collection.
Example:
public static void Main()
{
foreach (var employee in Get())
{
Console.WriteLine(employee.Name);
}
Console.ReadLine();
}
public static Ienumerable<Employee> 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 the 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 a specific number of the 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 upcoming chapters, we will discuss all these LINQ generation operations in detail with examples.