Reputation: 2719
This statement won't compile:
query = from g in context.GridViews
join f in context.GridViewFavorites on g.ID equals f.GridViewID into gf
where g.GridTypeID == id && ( g.IsShared == true || g.RepID == me.clsRep.OID)
&& f.RepID == me.clsRep.OID
select g;
The compiler error is this (and it's underlining the last part of the where clause:
The name 'f' does not exist in the current context
It's logical SQL counterpart would be:
declare @RepID int
declare @GridTypeID int
select @RepID=15, @GridTypeID=5
select g.*,f.*
from
GridViews g
left outer join GridViewFavorites f on f.GridViewID = g.ID
where
g.GridTypeID = @GridTypeID and (g.IsShared = 1 or g.RepID == @RepID)
and f.RepID == @RepID
NOTE: per @hdv 's good catch the SQL sample should actually be:
select g.*,f.*
from
GridView g
left outer join GridViewFavorite f on f.GridViewID = g.ID and f.RepID = @RepID
where
g.GridTypeID = @GridTypeID and (g.IsShared = 1 or g.RepID = @RepID)
Upvotes: 0
Views: 127
Reputation: 164291
The where
clause acts on the joined result, so the f
variable is not in scope.
Upvotes: 0
Reputation: 171178
The left-join pattern for LINQ goes like this:
join f in context.GridViewFavorites on g.ID equals f.GridViewID into gf
from f in gf.DefaultIfEmpty() //missing
More information is available on Stack Overflow and Google.
Upvotes: 0
Reputation: 1500525
It's the "into" part of your join - once you've joined "into" a group, the join variable (f
in this case) is out of scope - you've got to use gf
instead. Alternatively, given that you're not actually using gf
in your query at all, maybe you should just get rid of the into gf
part entirely so it's a normal join instead of a group join.
However, that won't give you a left outer join. If you want a left outer join, you might want:
query = from g in context.GridViews
join f in context.GridViewFavorites on g.ID equals f.GridViewID into gf
from f2 in gf.DefaultIfEmpty()
where g.GridTypeID == id && (g.IsShared == true || g.RepID == me.clsRep.OID)
&& (f2 == null || f2.RepID == me.clsRep.OID)
select g;
Upvotes: 5