请参阅下面的2个版本的代码和它生成的sql.我想加载首选项,但我也想只带我的用户首选项,因此我使用WHERE子句.但是一旦我把它放进Go – 我就不再让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,};
没有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 – 坏 – 没有外部加入
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],[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来实现这一目标?
回答
这就是我需要做的事情(将条件移到连接本身)
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,};
解决方法
我不确定你想要达到的目标,但我相信你需要在以下情况下调整条件: 来自p在上下文中.参考 加入context.UserPreferences.Where(x => x.UserKey == user.ProviderUserKey&& (//你的其他条件) ) 进入外…
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。