请参阅以下2个版本的代码及其生成的SQL。我想加载首选项,但我也想只带我的用户首选项,因此我使用WHERE子句。但是一旦我把它放进去 - 我就不再让OUTER加入了。为什么呢?
var query = from p in context.Preferences
join up in context.UserPreferences on p.PreferenceKey equals up.PreferenceKey into outer
from up in outer.DefaultIfEmpty()
select new MobileRESTEntities.UserPreference
{
CreatedOn = (up == null) ? p.CreatedOn : up.CreatedOn,
UpdatedOn = (up == null) ? p.CreatedOn : (up.UpdatedOn ?? up.CreatedOn),
PreferenceId = p.PreferenceId,
Value = (up == null) ? p.ValueDefault : up.Value,
};
使用WHERE:
var query = from p in context.Preferences
join up in context.UserPreferences on p.PreferenceKey equals up.PreferenceKey into outer
from up in outer.DefaultIfEmpty()
where up.UserKey.Equals((int)user.ProviderUserKey)
&&
(
(up == null)
||
((up.UpdatedOn > lastSyncOn && up.UpdatedOn != null) || (up.CreatedOn > lastSyncOn))
)
select new MobileRESTEntities.UserPreference
{
CreatedOn = (up == null) ? p.CreatedOn : up.CreatedOn,
UpdatedOn = (up == null) ? p.CreatedOn : (up.UpdatedOn ?? up.CreatedOn),
PreferenceId = p.PreferenceId,
Value = (up == null) ? p.ValueDefault : up.Value,
};
没有WHERE - GOOD
SELECT
[Extent1].[PreferenceKey] AS [PreferenceKey],
CASE WHEN ([Extent2].[UserPreferenceKey] IS NULL) THEN [Extent1].[CreatedOn] ELSE [Extent2].[CreatedOn] END AS [C1],
CASE WHEN ([Extent2].[UserPreferenceKey] IS NULL) THEN [Extent1].[CreatedOn] WHEN ([Extent2].[UpdatedOn] IS NULL) THEN [Extent2].[CreatedOn] ELSE [Extent2].[UpdatedOn] END AS [C2],
[Extent1].[PreferenceId] AS [PreferenceId],
CASE WHEN ([Extent2].[UserPreferenceKey] IS NULL) THEN [Extent1].[ValueDefault] ELSE [Extent2].[Value] END AS [C3]
FROM [dbo].[MBLPreference] AS [Extent1]
LEFT OUTER JOIN [dbo].[MBLUserPreference] AS [Extent2] ON [Extent1].[PreferenceKey] = [Extent2].[PreferenceKey]
使用WHERE - BAD - 没有OUTER JOIN
exec sp_executesql N'SELECT
[Extent1].[PreferenceKey] AS [PreferenceKey],
[Extent2].[CreatedOn] AS [CreatedOn],
CASE WHEN ([Extent2].[UpdatedOn] IS NULL) THEN [Extent2].[CreatedOn] ELSE [Extent2].[UpdatedOn] END AS [C1],
[Extent1].[PreferenceId] AS [PreferenceId],
[Extent2].[Value] AS [Value]
FROM [dbo].[MBLPreference] AS [Extent1]
INNER JOIN [dbo].[MBLUserPreference] AS [Extent2] ON [Extent1].[PreferenceKey] = [Extent2].[PreferenceKey]
WHERE ([Extent2].[UserKey] = @p__linq__0) AND ((1 = 0) OR (([Extent2].[UpdatedOn] > @p__linq__1) AND ([Extent2].[UpdatedOn] IS NOT NULL)) OR ([Extent2].[CreatedOn] > @p__linq__2))',N'@p__linq__0 int,@p__linq__1 datetime2(7),@p__linq__2 datetime2(7)',@p__linq__0=15,@p__linq__1='0001-01-01 00:00:00',@p__linq__2='0001-01-01 00:00:00'
修改
是的,WHERE不能正常工作,但我的SQL看起来应该是这样的:
SELECT P.*
FROM dbo.MBLPreference P
LEFT OUTER JOIN dbo.MBLUserPreference UP ON P.PreferenceKey = UP.PreferenceKey
AND UP.UserKey = 8 AND UP.CreatedOn > '1-1-1'
我应该如何编写LINQ来实现这一目标?
ANSWER
这就是我需要做的事情(将条件移到连接本身)
var query = from p in context.Preferences
join up in context.UserPreferences
.Where(x =>
x.UserKey.Equals((int)user.ProviderUserKey)
&&
((x.UpdatedOn > lastSyncOn && x.UpdatedOn != null) || (x.CreatedOn > lastSyncOn))
)
on p.PreferenceKey equals up.PreferenceKey into outer
from up in outer.DefaultIfEmpty()
select new MobileRESTEntities.UserPreference
{
CreatedOn = (up == null) ? p.CreatedOn : up.CreatedOn,
UpdatedOn = (up == null) ? p.CreatedOn : (up.UpdatedOn ?? up.CreatedOn),
PreferenceId = p.PreferenceId,
Value = (up == null) ? p.ValueDefault : up.Value,
};
答案 0 :(得分:1)
我不确定您要实现的目标,但我相信您需要在where
中移动条件:
from p in context.Preferences
join up in context.UserPreferences.Where(x=>x.UserKey ==user.ProviderUserKey &&
(// your other conditions)
)
into outer ....