开发者

Nullable int not working as expected in LINQ (C#)

开发者 https://www.devze.com 2023-02-24 08:38 出处:网络
The following works as expected (LINQ to Entities): var topics = (from t in ctx.Topics where t.SubjectId == subjectId && t.ParentId == null select new { t开发者_JS百科.Title, t.Id }).ToList()

The following works as expected (LINQ to Entities):

var topics = (from t in ctx.Topics where t.SubjectId == subjectId && t.ParentId == null select new { t开发者_JS百科.Title, t.Id }).ToList();

However, the following returns nothing:

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

Topic.ParentId is a nullable int. It's easy to work around, but this puzzles me. Can anyone shed any light?


You are definately not the first person to observe this... interesting... behavior.

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

In short, it's difficult to handle different ways of expressing null.

0

精彩评论

暂无评论...
验证码 换一张
取 消