none
Implementing outer joins using IEnumerable<T>

    Question

  • I am trying to implement the outer join using IEnumerable<T>.DEfaultIfEmpy(). But not sure where went wrong. Help me to achieve this.

    Sample Code:

     var microsoft = new Company() { CompanyName = "Microsoft" };
                this.CompanyList.Add(microsoft);
                var google = new Company() { CompanyName = "Google" };
                this.CompanyList.Add(google);
                var apple = new Company() { CompanyName = "Apple" };
                this.CompanyList.Add(apple);
                var samsung = new Company() { CompanyName = "Samsung" };
                this.CompanyList.Add(samsung);

                this.EmployeeList.Add(new Employee(microsoft.CompanyID) { EmployeeFirstName = "Bill", EmployeeLastName = "Gates", Age = 58 });
                this.EmployeeList.Add(new Employee(microsoft.CompanyID) { EmployeeFirstName = "Paul", EmployeeLastName = "Allen", Age = 61 });
                this.EmployeeList.Add(new Employee(microsoft.CompanyID) { EmployeeFirstName = "Steven", EmployeeLastName = "Ballmer", Age = 58 });
                this.EmployeeList.Add(new Employee(google.CompanyID) { EmployeeFirstName = "Larry", EmployeeLastName = "Page", Age = 41 });
                this.EmployeeList.Add(new Employee(google.CompanyID) { EmployeeFirstName = "Sergey", EmployeeLastName = "Brin", Age = 40 });
                this.EmployeeList.Add(new Employee(apple.CompanyID) { EmployeeFirstName = "Stephen", EmployeeLastName = "Wozniak", Age = 64 });
                this.EmployeeList.Add(new Employee(apple.CompanyID) { EmployeeFirstName = "Ronald ", EmployeeLastName = "Wayne ", Age = 80 });
                this.EmployeeList.Add(new Employee(apple.CompanyID) { EmployeeFirstName = "Steven", EmployeeLastName = "Jobs", Age = 58 });
                this.EmployeeList.Add(new Employee(Guid.NewGuid()) { EmployeeFirstName = "Reza", EmployeeLastName = "Hedayati", Age = 31 });

    Method 1: 

    var leftDefaultSyntax = from emp in EmployeeList
                                        join comp in CompanyList on emp.CompanyID equals comp.CompanyID into EmpComp
                                        from EmpCompRes in EmpComp.DefaultIfEmpty()
                                        select new
                                        {
                                            EmployeeId = emp.CompanyID,
                                            EmployeeName = string.Format("{0},{1}", emp.EmployeeFirstName, emp.EmployeeLastName),
                                            Age = emp.Age,
                                            CompanyId = EmpCompRes == null ? Guid.Empty : EmpCompRes.CompanyID,
                                            CompanyName = EmpCompRes == null ? string.Empty : EmpCompRes.CompanyName
                                        };
                foreach(var empComp in leftDefaultSyntax)
                    Console.WriteLine("Name:{0}, Age:{1}, Company:{2}", empComp.EmployeeName, empComp.Age, empComp.CompanyName);

    Method 2:

                

    List<Result> leftJoin = null;
                leftJoin = EmployeeList.Join(CompanyList, Emp => Emp.CompanyID, Com => Com.CompanyID, (Emp, Comp) => new
                {
                    EmployeeID = Emp.EmployeeID,
                    EmployeeName = string.Format("{0},{1}", Emp.EmployeeLastName, Emp.EmployeeFirstName),
                    Age = Emp.Age,
                    CompanyID = Comp.CompanyID,
                    CompanyName = Comp.CompanyName
                }).DefaultIfEmpty().Select(data => new Result
                {
                    EmployeeID = data.EmployeeID,
                    EmployeeName = data.EmployeeName,
                    Age = data.Age,
                    CompanyID = data == null ? Guid.Empty : data.CompanyID,
                    CompanyName = data == null ? string.Empty : data.CompanyName
                }).ToList<Result>();

                foreach (Result res in leftJoin)
                    Console.WriteLine("Name:{0}, Age:{1}, Company:{2}", res.EmployeeName, res.Age, res.CompanyName);

    Method 1 is giving expected result, where Method 2 not. Help me to achieve the Method 1 result using Method 2.

    Friday, April 22, 2016 7:13 AM

Answers

All replies