title: LINQ "equals" operator on nullable foreign key in NHibernate parent/child relationships
body: <h3>The background</h3>
  <p>My model looks like the following: (writing fields instead of properties for simplicity)</p>
  <pre><code>public class Entity {
      public long Id;
      public string Name;
      public Entity Parent;
  }
  </code></pre>
  <p>Essential FNH mapping</p>
  <pre><code>        Map(x =&gt; x.Name)
              .Not.Nullable()
              .UniqueKey(&quot;Child&quot;);
  
          References(x =&gt; x.Parent)
              .Cascade.None()
              .UniqueKey(&quot;Child&quot;);
  </code></pre>
  <p>SQL</p>
  <pre><code>create table `Entity` (Id BIGINT not null, Name VARCHAR(255) not null, Parent_id BIGINT, primary key (Id),unique (Name, Parent_id))
  </code></pre>
  <p>And that's fine. I don't want omonimities between children of the same entities (so two entities of different parent may have same name). By the way, bear in mind that <code>Parent_id</code> is <strong>nullable</strong></p>
  <h3>What I need to do</h3>
  <p>I want to enforce a check before inserting a new entity into the DB. Instead of catching an exception I want to fire a query (but I think it reduces performance...) to check if an entity with same name and parent of newcoming exists in order to throw a decent exception. Despite performance, it's still a chance to learn something new about LINQ providers</p>
  <p>In plain old SQL I would do</p>
  <pre><code>SELECT Id FROM entity WHERE Name = ? AND Parent_id = ?
  </code></pre>
  <p>and this correctly supports NULL ids</p>
  <h3>What I tried (and failed, otherwise I wouldn't be here)</h3>
  <pre><code>var exInput = (from Entity entity in entityRepository.Query()
      where entity.Name.ToLowerInvariant() == _newEntity.Name.ToLowerInvariant()
          &amp;&amp; entity.Parent.Equals(_newEntity.Parent)
      select new { ParentName = entity.Parent != null ? entity.Parent.Name : null }).FirstOrDefault();
  </code></pre>
  <p>I thought NHibernate could be smart enough to accept a null value as <code>_newEntity.Parent</code> and also smart enough to read <code>entity.Parent.Equals</code> as an expression instead of a method call (which fails in case of null).</p>
  <p>Anyway that's not the problem</p>
  <h3>The error</h3>
  <pre><code>System.NotSupportedException: Boolean Equals(System.Object)
  </code></pre>
  <p>I know NHibernate LINQ is not a full LINQ provider and doesn't support all methods Entity Framework supports. So I could expect that. Obviously, I <strong>can</strong> workaround by first selecting entity by name and then check if both parents are null or if they <code>Equals()</code> (I overloaded <code>Equals</code> to check Id)</p>
  <h3>The question</h3>
  <p>Given that I want NHibernate to generate SQL as closest as possible to the above <code>WHERE</code> clause, what should I do? Is there a different LINQ syntax to use or should I extend the LINQ provider?</p>
  <p>I was thinking about extending LINQ provider, for which I have found some documentation. It is my opinion that if the operands of comparison are of the same identity we can simply match their ID (and if one of the entities is <code>null</code> generate <code>NULL</code> identity in HQL). In this case, did anyone try an implementation to share?</p>