0

Although this seems very basic but i am struggling a lot in getting the concept,how actually left join works?and what is the purpose of into keyword meaning when there is no chance of grouping then why it is used to achieve left join??how and what we are grouping here??

public class LinqJoinController : Controller
    {
        //
        // GET: /LinqJoin/

        public List<Department> getAllDepartment()
        {
            return new List<Department>(){
                new Department{Id=1,Name="IT"},
                 new Department{Id=2,Name="HR"},

            };
        }

        public List<Employee> getAllEmployee()
        {
            return new List<Employee>(){
                new Employee{Id=1,Name="Sachin",DepartmentId=1},
                 new Employee{Id=2,Name="Ram",DepartmentId=1},
                  new Employee{Id=3,Name="Shyam",DepartmentId=2},
                 new Employee{Id=4,Name="Radhe"},
            };
        }
        public ActionResult Index()
        {
            var result = from  emp in getAllEmployee()
                         join dep in getAllDepartment()

                             on emp.DepartmentId equals dep.Id 
                             into eGroup
                             from dep in eGroup.DefaultIfEmpty()
                         select new EmployeeVM
                         {
                             DepartmentName = dep==null?"No Dept":dep.Name,
                             EmployeeName = emp.Name
                         };
            return View(result);
        }
    }
}
Sachin Singh
  • 204
  • 1
  • 12
  • The duplicate should help you grasp the concept, even though it's in a slightly different context. Also, you're question is very broad. You're just firing away a couple of things you don't understand. That doesn't work well at Stack Overflow. – Gert Arnold Jun 29 '18 at 14:44
  • ofcourse the department in my example can contain list of employee so grouping is possible but not the reverse is true.so why we use into.i did not get answer in duplicate ,that you reffered @GertArnold – Sachin Singh Jun 29 '18 at 14:51
  • Come on, you took only 7 minutes to try and grasp a totally new concept? There's no shortcut to mastering concepts. You have to take the time and experiment with these LINQ methods yourself, just as I did when LINQ was first released. For example [here](https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b). – Gert Arnold Jun 29 '18 at 14:58

0 Answers0