我有以下编译的Linq查询:
public static readonly Func<DBContext, Models.User, Type, ObjectType, int?, UserNotification> GetUnreadNotificationID =
CompiledQuery.Compile((DBContext db, Models.User forUser, Type notificationType, ObjectType forObjectType, int? forObjectID) =>
db.UserNotifications.FirstOrDefault(c =>
c.ForUserID == forUser.ID
&& c.ForObjectTypeID == (short)forObjectType
&& c.ForObjectID == forObjectID
&& c.TypeID == (byte)notificationType
&& c.Date > forUser.NotificationsLastRead.Date));
请注意参数int? forObjectID
。
在查询分析器中,执行的SQL语句示例如下:
exec sp_executesql N'SELECT TOP (1)
[t0].[ID], [t0].[TypeID], [t0].[ForUserID], [t0].[Date], [t0].[ForObjectTypeID], [t0].[ForObjectID], [t0].[Count]
FROM
[dbo].[UserNotifications] AS [t0]
WHERE
([t0].[ForUserID] = @p0)
AND ([t0].[ForObjectTypeID] = @p1)
AND ([t0].[ForObjectID] = @p2)
AND ([t0].[TypeID] = @p3)
AND ([t0].[Date] > @p4)',
N'@p0 int,@p1 int,@p2 int,@p3 int,@p4 datetime',@p0=77812,@p1=5,@p2=NULL,@p3=4,@p4='2018-01-24 13:18:44.107'
当forObjectID
为null
时,查询不会返回预期的记录。如果我改变:
AND ([t0].[ForObjectID] = @p2)
要:
AND ([t0].[ForObjectID] IS NULL)
确实会返回正确的结果。
答案 0 :(得分:1)
如果这是Linq2Sql
变化
c.ForObjectID == forObjectID
至Object.Equals(c.ForObjectID, forObjectID)
,以便is null
为空时能够翻译为forObjectID
。
答案 1 :(得分:0)
简单的解决方法是编写一个SQL存储过程,它可以执行您想要的操作 - 我怀疑有人会修复LinkToSql。
或者试试这个:
((c.ForObjectID == forObjectID) ||
(c.ForObjectId == null && forObjectId == null))