简体   繁体   English

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

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

The following works as expected (LINQ to Entities): 以下按预期工作(LINQ to Entities):

var topics = (from t in ctx.Topics where t.SubjectId == subjectId && t.ParentId == null select new { t.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. Topic.ParentId是一个可以为null的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 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. 简而言之,很难处理表达null的不同方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM