Accepted answer

you need to do like this:

 var result = from at in db.assettagging
              join r in db.returns on at.assetid equals r.assetid into a
              from returns into a.defaultifempty()
              join i in db.issues on at.assetid equals i.assetid into b
              from issues into b.defaultifempty()
              where issues.assetid != null || returns.requeststatus == "approved"
              select new
                      assetid = at.assetid,
                      assetdescription = at.assetdescription,
                      status = returns != null ? returns.requeststatus : null


here is the complete query

  var result = (from assettagging in db.assettagging
                join return0 in db.return on assettagging.assetid equals return0.assetid into returns
                from return0 in returns.defaultifempty()
                join issue in db.issue on assettagging.assetid equals issue.assetid into issues
                from issue in issues.defaultifempty()
                where issue.assetid == null || return0.requeststatus == "approved"
                select new


try the following i am assuming that you still want the cases where r is null unless r is not null and request status = approved.

you have to check to verify r!=null before checking the request status and you will still need to include when r is null to get the complete result set. i haven't tested this, but this should put you in the right direction.

good luck.

  var result = (from at in db.assettagging
               join r in db.return.defaultifempty()
               on at.assetid equals r.assetid
               join i in db.issue.defaultifempty()
               on  at.assetid equals i.assetid
               (r == null || (r!=null && r.requeststatus == "approved"))
               || i == null
               select new {
                issueid = (i!=null) ? i.issueid : null),
                returnid = (r!=null) ? r.returnid: null),
                returnstatus = (r!=null) 
                     ? r.returnstatus 
                     : null}).tolist();


try like following:

from at in db.assettagging
join r in db.return on at.assetid equals r.assetid into res1
from atr in res1.defaultifempty()
join  i in db.issues on i.assetid==at.assetid into res2
from obj in res2.defaultifempty()
select at
where i.assetid == null || r.requeststatus equals "approved"

just make two times left outer join and then do filter on where condition.

also have first look at this msdn article about left outer join using linq.


i know this isn't exactly what you've asked for, but it might be useful anyway.

if you have access to the database to execute sql queries, i would suggest creating a view. you can then drop the view into your dbml file the same way as you would with a table, and have much simpler linq expressions in your c# code.

create view [asset_issue_return_joined] as
select assettagging.assetid, assettagging.assetdescription, [return].requeststatus
from  assettagging
left outer join [return] on assettagging.assetid = [return].assetid
left outer join issue on assettagging.assetid = issue.assetid
where (issue.assetid is null) or ([return].requeststatus = 'approved')


you need to remove .asenumerable(), because you want your query to be translated to sql. right now it would be using linq-to-objects and if you are using a left join with linq-to-object you need to check for null reference exceptions. rt could be null, so rt.requeststatus would throw an exception.

*i believe rt should be r in your example

you can't project to an existing entity, so you need to change your select to:

select new pococlass

//new class definition
public pococlass
  public assettagging model1 { get; set; }

Related Query

More Query from same tag