简体   繁体   中英

Why doesn't using a nullable Guid work in this linq query?

I have this code to find the root node of a tree:

Guid? currentNode = null; 
var root = db.RecursiveTrees.Where(x => x.ParentId == currentNode).ToList();

This query returns 0 results.

If I run this query I get the expected row returned:

var root = db.RecursiveTrees.Where(x => x.ParentId == null).ToList();

Why doesn't the first query work (using the latest version of entity framework)?

EDIT:

Workaround:

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();     

This is a known bug in LINQ to Entities when dealing with nullable value-types. According to the relevant Connect issue , this will be fixed in the next release.

try this

Guid? currentNode = null; 
var root = db.RecursiveTrees.Where(x => x.ParentId == currentNode.Value).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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