2012-08-28 67 views
0

我试图选择'Value'列为空或空白的所有行。它是nvarchar类型,允许为空值。查询执行为1等于1而不是空值检查

执行时,以下查询不会生成正确的sql。

current.Where(
      a => 
      a.Data.All(ad => ad.AccountTag.Id != currentTag.Item1) 
      || a.Data.Any(ad => ad.AccountTag.Id == currentTag.Item1 && string.IsNullOrWhiteSpace(currentTag.Item2))) 

的string.IsNullOrWhiteSpace功能转换为1 /* @p3 */ = 1

我使用上述功能,并且还使用currentTag.Item2 == null || currentTag.Item2.Equals(string.Empty)都具有相同的结果尝试。

完整的SQL下面

select data2_.Id from [isnapshot.Client].[dbo].AccountData data2_ 
    where account0_.Id = data2_.ClientAccountId 
    and data2_.AccountTagId = 1 /* @p2 */ 
    and 1 /* @p3 */ = 1) 

回答

2

查询是正确的。 currentTag是与查询无关的局部变量。因此,表达式string.IsNullOrWhiteSpace(currentTag.Item2)将在之前被评估为,它甚至会被转换为SQL,结果为true(1)或false(0),这将是值p3

+0

只有当别人看你的代码时,你才会意识到你是个白痴。 'currentTag.Item2'应该是'ad.Value'如果你没有指出它,永远不会注意到。谢谢 – JConstantine

+0

@JLevett:有时候你只是对最明显的事情失明。偶尔每个人都会遇到这种情况:-) –

相关问题