为什么在这个linq查询中没有使用可以为空的Guid工作?

wog*_*les 5 c# linq asp.net entity-framework

我有这个代码来查找树的根节点:

Guid? currentNode = null; 
var root = db.RecursiveTrees.Where(x => x.ParentId == currentNode).ToList();
Run Code Online (Sandbox Code Playgroud)

此查询返回0结果.

如果我运行此查询,我会返回预期的行:

var root = db.RecursiveTrees.Where(x => x.ParentId == null).ToList();
Run Code Online (Sandbox Code Playgroud)

为什么第一个查询不起作用(使用最新版本的实体框架)?

编辑:

解决方法:

List<RecursiveTree> root;
if (nodeid == null)
   root = db.RecursiveTrees.Where(x => x.ParentId == null).ToList();
else
   root = db.RecursiveTrees.Where(x => x.ParentId == new Guid(nodeid)).ToList();     
Run Code Online (Sandbox Code Playgroud)

Ani*_*Ani 10

在处理可空值的类型时,这是LINQ to Entities中的一个已知错误.根据相关的Connect问题,这将在下一个版本中修复.

  • 它仍未在EF 6.1.3中修复.如果它被修复了,它又被打破了. (2认同)