title: FluentNHibernate mapping with a filter, is it possible?
body: <p>I'm new to NHibernate, and FluentNHibernate. I'm trying to figure out the best way to setup mappings between tables in different schemas as well as potentially doing some filtering in the mappings. </p>
  
  <p>In my scenario I have doctors who are associated with hospitals where a doctor can be associated with multiple hospitals. At some hospitals a doctor might be blacklisted from performing certain procedures. There are also verying degrees of blacklisting and I'm only interested in those whose black list type is zero. Because the tables reside in different schemas and are managed by different IT groups the column names for joining don't always match. I'm trying to figure out the best way to map that relationship. </p>
  
  <p>I have part of this working, but I'm not sure how to get the whole relationship setup so that I only get those hospitals where a doctor has a specific black list designation.</p>
  
  <p>Here's a trimmed down look at my entities.</p>
  
  <pre><code>public class Doctor
  {
      pubilc virtual int DoctorId { get; set; }
      public virtual string Name { get; set; }
      public virtual IList&lt;BlackListSite&gt; BlackListedSites { get; set; }
  }
  
  public class BlackListSite
  {
      public virtual int DoctorId { get; set; }
      public virtual int HospitalId { get; set; }
      public virtual int ProcedureId { get; set; }
      public virtual int BlackListTypeId { get; set; }
      public virtual IList&lt;Facility&gt; Facilities { get; set; } //these are the hospitals
  }
  
  public class Facility //this represents a hospital
  {
      public virtual int FacilityId{ get; set; }
      public virtual string Name { get; set; }
  }
  </code></pre>
  
  <p>Here's how I'm mapping the BlackListSite and Hospital, which is working.</p>
  
  <pre><code>public class BlackListSitesMap : ClassMap&lt;BlackListSites&gt;
  {
      public BlackListSitesMap()
      {
          Schema("schema1");
          Table("RestrictedDoctors");
          Id(x =&gt; x.BlackListId).GeneratedBy.Identity();
          Map(x =&gt; x.DoctorId);
          Map(x =&gt; x.HospitalId);
          Map(x =&gt; x.Status);
          HasMany(x =&gt; x.Facilities).Cascade.All().Not.LazyLoad()
                      .KeyColumns.Add("HospitalID", 
                                  mapping =&gt; mapping.Name("FacilityID"));
      }
  }
  
  public class FacilityMap : ClassMap&lt;Facility&gt;
  {
      public FacilityMap()
      {
          Schema("schema2");
          Table("Facility");
          Id(x =&gt; x.FacilityId);
          Map(x =&gt; x.Name);
          Map(x =&gt; x.Active);
      }
  }
  </code></pre>
  
  <p>A query like this gets me a list of blacklisted sites where blacklist type is 0.</p>
  
  <pre><code>public void TestBlackList()
  {
      using (var session = SessionManager.Session())
      {
          var blacklist = session.Query&lt;BlackListSites&gt;().
                      Where(b =&gt; b.DoctorId == 1 &amp;&amp; b.HospitalId != null 
                                  &amp;&amp; b.Status == 0).ToList();
      }
  }
  </code></pre>
  
  <p>I'm not sure how to tie the blacklisted sites to the Doctor so that when I request a doctor I get a list of sites where the doctor is blacklisted with a blacklist type of zero. Any ideas?</p>
  
  <p>If I can't do this on the mapping, can I somehow setup the query on Doctor to filter the blacklist where BlackListTypeId = 0? That may be the most ideal anyway. It would set me up in the future to request other Blacklist types.</p>