Nullable int在LINQ(C#)中没有按预期工作

Jam*_*mes 5 c# linq linq-to-entities entity-framework

以下按预期工作(LINQ to Entities):

var topics = (from t in ctx.Topics where t.SubjectId == subjectId && t.ParentId == null select new { t.Title, t.Id }).ToList();
Run Code Online (Sandbox Code Playgroud)

但是,以下内容不返回:

int? parent = null;
var topics = (from t in ctx.Topics where t.SubjectId == subjectId && t.ParentId == parent select new { t.Title, t.Id }).ToList();
Run Code Online (Sandbox Code Playgroud)

Topic.ParentId是一个可以为null的int.这很容易解决,但这让我很困惑.谁能摆脱任何光明?

The*_*eze 4

您绝对不是第一个观察到这种……有趣的……行为的人。

http://connect.microsoft.com/data/feedback/details/607404/entity-framework-and-linq-to-sql-in Correctly-handling-nullable-variables

简而言之,处理 null 的不同表达方式是很困难的。