此语句不会编译:
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;
编译器错误是这样的(它在 where 子句的最后一部分下划线:
当前上下文中不存在名称“f”
它的逻辑 SQL 对应物是:
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
注意:根据@hdv 的好消息,SQL 示例实际上应该是:
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)