locked
Guid Null Comparison issues RRS feed

  • Question

  • I have what seems to be a simple linq statement:

     

    Guid? parentGuid = null;

                if (root != null)
                    parentGuid = root.ParentGuid;

                var nodes = (from n in _db.Navigations where n.ParentGuid == parentGuid select n);

     

    The problem is if the parent Guid is null, linq does not output sql correctly as I would expect ie. "select * from table where ParentGuid Is Null".  It uses a comparison operator instead (select * from table where ParentGuid = @p1), which will not return rows because ansii_nulls is always set to on by default, and from what I understand I can't change this programmically without creating custom stored procs.  I've tried changing ansi_nulls to off on my database, but the ado.net connection overrides the default.

     

    Anyone have a solution to this problem?  Thanks in advance.

     

     

    Wednesday, April 2, 2008 5:13 PM

Answers

  •  

    Guid? parentGuid = null;

                if (root != null)
                    parentGuid = root.ParentGuid;

                var nodes = (from n in _db.Navigations where n.ParentGuid == parentGuid || (n.ParentGuid == null && parentGuid == null) select n);

    Thursday, April 3, 2008 1:40 AM

All replies

  •  

    Guid? parentGuid = null;

                if (root != null)
                    parentGuid = root.ParentGuid;

                var nodes = (from n in _db.Navigations where n.ParentGuid == parentGuid || (n.ParentGuid == null && parentGuid == null) select n);

    Thursday, April 3, 2008 1:40 AM
  •  

    Thanks alot Kris, this was exactly what I was looking for.
    Thursday, April 3, 2008 5:39 PM