0

I searched hours and hours for this without any luck. I'm trying to create a lambda expression to fetch data from two tables Schedule and Request. But i'm outputting a bool here. How can i do a proper left outer join to fix this? this is the best i could come up with

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

but its a bool not a list.

Assume my table models are as follows

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'm trying to see if each and every request has one or more schedules associated with it or not. so if i could attach schedule object to request and output it as a list then thats all i need. But I want to do it using LINQ and lambda expressions and I've seen queries as 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 that's 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 }

Thanks a lot!

1
  • yes ScheduleId=j==null?0:j.ScheduleId will do but, see my comment on your answer. Commented Aug 22, 2014 at 7:46

1 Answer 1

1

just list what you want like this:

          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,
                            };
Sign up to request clarification or add additional context in comments.

3 Comments

As i clearly stated in my question i don't want to specify each and every property inside select new{} because my original model class Request has many number of properties. is there a way i could fetch all fields like we use * in sql?
I'm afraid there is no simple way to use select * for LINQ, you can try this select new { emp,j}
The only way is select emp or select j or select {emp,j}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.