2009-12-12 53 views
7

我已經使用Castel Active Record實現了一個搜索功能。我認爲代碼是很簡單,但我一直得到帶命名參數的nhibernate hql

NHibernate.QueryParameterException:無法找到名爲參數[searchKeyWords]

錯誤。有人能告訴我哪裏出了問題嗎?太感謝了。

public List<Seller> GetSellersWithEmail(string searchKeyWords) 
     { 
      if (string.IsNullOrEmpty(searchKeyWords)) 
      { 
       return new List<Seller>(); 
      } 
      string hql = @"select distinct s 
          from Seller s 
          where s.Deleted = false 
            and (s.Email like '%:searchKeyWords%')"; 

      SimpleQuery<Seller> q = new SimpleQuery<Seller>(hql); 
      q.SetParameter("searchKeyWords", searchKeyWords); 
      return q.Execute().ToList(); 
     } 

回答

13

爲什麼不傳遞%字符的參數?

string hql = @"select distinct s 
          from Seller s 
          where s.Deleted = false 
            and (s.Email like :searchKeyWords)"; 
    SimpleQuery<Seller> q = new SimpleQuery<Seller>(hql); 
    q.SetParameter("searchKeyWords", "%"+searchKeyWords+"%"); 
    return q.Execute().ToList(); 
+0

我還沒有證實你的解決方案,但是我從 得到了類似的答案 http://www.stpe.se/2008/07/hibernate-hql-like-query-named-parameters/ 並且該解決方案起作用。所以,我會假設你的也是對的。非常感謝。 – 2009-12-12 02:06:25