Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using Union All in Linq

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.20k
    Comment on it

    This is a simple example of how we use UNION ALL in Linq.


    In fact there is no UNION ALL operator in Linq but we have an equivalent of UNION ALL as Concat operator.


    So suppose we have two list (oldEmployees and newEmployees) which hold name and salary data of employees. One(oldEmployees) is for keeping the old employee records and other(newEmployee) is used for keeping the records of new employee.


    Now we have the requirement where we want to get the data of both list in one list. If it had to be done on sql part then we could easily use UNION ALL but here we have to do it using Linq.


    So for Linq we will use Concat operator to yield the desired list. Here is the sample code for the same.


     class Program
        {
            static void Main(string[] args)
            {
                List<Employee> newEmployees = new List<Employee>();
                List<Employee> oldEmployees = new List<Employee>();
                List<Employee> allEmployees = new List<Employee>();
    
    
                newEmployees.Add(new Employee
                {
                    Name = "Abhishek",
                    Salary = 40500
                }
                                );
    
                oldEmployees.Add(
                    new Employee
                    {
                        Name = "Amit",
                        Salary = 50000
                    }
                    );
    
                allEmployees = ((from ne in newEmployees
                                 select new Employee
                                 {
                                     Name = ne.Name,
                                     Salary = ne.Salary
                                 }).Concat
                                (from oe in oldEmployees
                                 select new Employee
                                 {
                                     Name = oe.Name,
                                     Salary = oe.Salary
                                 })
                                .ToList()).ToList();
    
            }
        }
    
        public class Employee
        {
            public string Name;
            public int Salary;
        }
    

    In above code we have a class that has fields for Employee Name and Salary. In Main method of the program we are adding one employee each in oldEmployee and newEmployee(list of Employee class). After that we are fetching the records of both these lists in allEmployees list, which is again a list of Employee class, using Concat operator of Linq. Now allEmployees will have all the records of both lists


    This way we can fetch records from multiple lists in one list.


    Note To use Concat, the select statement object has to be of the same type in select statements of both lists.


    I hope this will help you in similar requirements. Happy coding.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: