How to create simple left outer join in lambda expression in EF ASP.NET MVC5

I have looked for hours and hours for this with no luck. I am trying to create a lambda expression to retrieve data from two tables Schedule and Request. But I was outputting bool here. How can I do the correct left outer join to fix this? this is the best i could think of

ViewBag.RequestList = db.Requests
    .Include(r => r.Department)
    .Select(r => db.Schedules.Any(s => s.RequestId == r.RequestId));

      

but its bool is not a list.

Suppose my table models look like this

public class Request{
 public virtual int RequestId { get; set; }
 public virtual string Remarks { get; set; }
}

public class Schedule{
 public virtual int ScheduleId{ get; set; }
 public virtual string Name{ get; set; }
 public virtual Request Request { get; set; }
}

      

I am trying to see if each request has one or more graphs associated with it or not. so if I can hook up a schedule object for a query and display it as a list, then that's all I need. But I want to do it with LINQ and lambda expressions and I have seen queries like below:

var leftList = (from emp in db.Requests
                join d in db.Schedules
                on emp.RequestId equals d.RequestId into output
                from j in output.DefaultIfEmpty()
                select new { RequestId = emp.RequestId, 
                             name = emp.Department.Name, 
                             route = emp.Route.Name });

      

But this is not what I want, because I have to specify every field I need in new { RequestId = emp.RequestId, name = emp.Department.Name, route = emp.Route.Name }

Thank you so much!

0


source to share


1 answer


just indicate what you want:



          var leftList =    from emp in db.Requests
                            join d in db.Schedules
                            on emp.RequestId equals d.RequestId into output
                            from j in output.DefaultIfEmpty()
                            select new 
                            { 
                               RequestId = emp.RequestId,
                               name = emp.Department.Name, 
                               route = emp.Route.Name,
                               ScheduleId=j==null?0:j.ScheduleId,
                               SName=j==null?""j.Name,
                            };

      

0


source







All Articles