2013-03-18 57 views
1

我有一個病人記錄將會/不會有設備歷史記錄的情況。我需要查找所有沒有設備記錄的患者記錄或所有設備記錄中具有非空字段的患者記錄。我在下面查詢的方式不起作用。我沒有發現任何沒有設備歷史的病人。有什麼建議麼?映射是正確的,因爲我可以直接訪問記錄,更新等。由於我有一個由40,000多條記錄組成的數據庫,因此我無法撤回所有患者並進行小孩計數。這會很慢,並使用太多的內存。寫查詢找到沒有孩子的父母

任何幫助將不勝感激。提前致謝。

public IList<Patients> GetAllPatientsWithoutDevice(int facilityID, string search) 
    { 
     ICriteria query = FluentSessionManager.GetSession() 
      .CreateCriteria<Patients>() 
      .Add(Expression.Eq("IsDeleted", false)) 
      .CreateAlias("Facilities", "f") 
      .Add(Expression.Eq("f.ID", facilityID)) 
      .CreateAlias("EquipmentHistory", "eh") // Tried, inner, left and right joins... 
      .Add(Expression.Or(
       Expression.IsNull("EquipmentHistory"), 
       Expression.IsNotNull("eh.DateOffPatient") 
      )); 

     query.AddOrder(new Order("FirstName", true)) 
      .AddOrder(new Order("LastName", true)); 

     return query.List<Patients>(); 
    } 

回答

1

我想通了......我不得不使用「IsEmpty」而不是「IsNull」。

ICriteria query = FluentSessionManager.GetSession() 
      .CreateCriteria<Patients>() 
      .Add(Expression.Eq("IsDeleted", false)) 
      .CreateAlias("Facilities", "f") 
      .Add(Expression.Eq("f.ID", facilityID)) 
      .CreateAlias("EquipmentHistory", "eh", NHibernate.SqlCommand.JoinType.LeftOuterJoin) 
      .Add(Expression.Or(
       Expression.IsEmpty("EquipmentHistory"), 
       Expression.IsNotNull("eh.DateOffPatient") 
      ));