I have a query where I need to get the value from a view only if there are no results returned from a table used in the right side of a left outer join.
Let's take 'left' to be the left table in the join, 'right' to be the right table and 'optional' to be populated with a value only if there is no match between 'left' and 'right'.
from left in ctx.tblLeft
from right in ctx.tblRight.Where(x=>predicate(x)).DefaultIfEmpty()
let optional =
(from v in ctx.vwMyView
where right==null && criteria
select new anonType { x = v.prop }).FirstOrDefault();
//select projection
However, that would mean that an "outer apply" query will be generated for my view. Wouldn't this impact my performance or is there a way that I can omit the execution of the view itself?
So what I would ideally like would look like this:
from left in ctx.tblLeft
from right in ctx.tblRight.Where(x=>predicate(x)).DefaultIfEmpty()
let optional = right!=null ? null : //only execute query if the condition is false
(from v in ctx.vwMyView
where criteria
select new anonType { x = v.prop }).FirstOrDefault();
//select projection
However the issue is that EF would not know how to covert an object of anonType returned by my view into a "null" since only value, enums etc. are supported. Are there any other ways of getting this to work?