Author : Rahul
Last Modified : 13-Jul-2021
Complexity : Beginner
Miscellaneous operator in LINQ
Miscellaneous Operator in LINQ is used to combine two or more collections into one collection or check whether two collections are equal or not. There are different Miscellaneous Operator are available in LINQ as follow:
- CONCAT
- SEQUENCEEQUAL
CONCAT Operator
CONCAT Operator in LINQ is used to combine two or more collections into one collection. Concat keyword is used for this purpose. Below are some examples in this category.
Query to create a sequence that contains each array value, one after the other.
class Program
{
static void Main(string[] args)
{
int[] numbersA = { 3, 6, 8, 9 };
int[] numbersB = { 3, 5, 6, 8 };
//Linq Query
var result_Linq = numbersA.Concat(numbersB);
//Lambda Expression
var result_Lambda = numbersA.Concat(numbersB);
//Display Linq Query Result
Console.WriteLine("Numbers from both array (Using Linq):");
foreach(var item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Numbers from both array (Using Lambda):");
foreach (var item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Numbers from both array (Using Linq):
3
6
8
9
3
5
6
8
Numbers from both array (Using Lambda):
3
6
8
9
3
5
6
8
Query to create a sequence that contains employee name and manager name, one after the other.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
List<Employee> managers = GetManagerList();
//Linq Query
var employee_Name = from p in employees select p.Emp_Name;
var manager_Name = from p in managers select p.Emp_Name;
var result_Linq = employee_Name.Concat(manager_Name);
//Lambda Expression
var employee_Name_L = employees.Select(p => p.Emp_Name);
var manager_Name_L = managers.Select(p => p.Emp_Name);
var result_Lambda = employee_Name_L.Concat(manager_Name_L);
//Display Linq Query Result
Console.WriteLine("Employee Names and Manager Name from both list (Using Linq):");
foreach (string item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Employee Names and Manager Name from both list (Using Lambda):");
foreach (string item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
/// <summary>
/// Method to get manager list
/// </summary>
/// <returns></returns>
private static List<Employee> GetManagerList()
{
List<Employee> managers = new List<Employee>();
managers.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 170000, Emp_Department = "IT" });
managers.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 120000, Emp_Department = "HR" });
managers.Add(new Employee() { Emp_Name = "Sandeep", Emp_Salary = 140000, Emp_Department = "IT" });
managers.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 180000, Emp_Department = "Account" });
return managers;
}
}
Output
Employee Names and Manager Name from both list (Using Linq):
Rahul
Amit
Suresh
Nitish
Rahul
Amit
Sandeep
Neeraj
Employee Names and Manager Name from both list (Using Lambda):
Rahul
Amit
Suresh
Nitish
Rahul
Amit
Sandeep
Neeraj
SEQUENCEEQUAL Operator
SEQUENCEEQUAL operator is used to return true if all elements within the collection are equal and their sequence within the collection is the same. SequenceEqual keyword is used for this purpose. Below are some examples in this category.
Query to find whether two arrays are the same or not.
class Program
{
static void Main(string[] args)
{
int[] numbersA = { 3, 6, 8, 9 };
int[] numbersB = { 3, 6, 8, 9 };
//Linq Query
var result_Linq = numbersA.SequenceEqual(numbersB);
//Lambda Expression
var result_Lambda = numbersA.SequenceEqual(numbersB);
//Display Linq Query Result
Console.WriteLine($"Both array are same (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Both array are same (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
Both array are same (Using Linq): True
Both array are same (Using Lambda): True
Query to find whether two arrays are the same or not.
class Program
{
static void Main(string[] args)
{
int[] numbersA = { 3, 6, 8, 9 };
int[] numbersB = { 3, 8, 6, 9 };
//Linq Query
var result_Linq = numbersA.SequenceEqual(numbersB);
//Lambda Expression
var result_Lambda = numbersA.SequenceEqual(numbersB);
//Display Linq Query Result
Console.WriteLine($"Both array are same (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Both array are same (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
Both array are same (Using Linq): False
Both array are same (Using Lambda): False