LINQ编译查询与nullable int斗争

时间:2018-01-24 13:28:27

标签: c# linq-to-sql int nullable isnull

我有以下编译的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'

forObjectIDnull时,查询不会返回预期的记录。如果我改变:

AND ([t0].[ForObjectID] = @p2) 

要:

AND ([t0].[ForObjectID] IS NULL)

确实会返回正确的结果。

  • 为什么null没有以我期望的方式处理?
  • 有一个简单的解决方法吗? (我可以将表转换为不接受该字段的空值,默认为0但感觉很icky)

2 个答案:

答案 0 :(得分:1)

如果这是Linq2Sql

变化
c.ForObjectID == forObjectIDObject.Equals(c.ForObjectID, forObjectID)

,以便is null为空时能够翻译为forObjectID

答案 1 :(得分:0)

简单的解决方法是编写一个SQL存储过程,它可以执行您想要的操作 - 我怀疑有人会修复LinkToSql。

或者试试这个:

((c.ForObjectID == forObjectID) || 
 (c.ForObjectId == null && forObjectId == null))
相关问题