2009-12-12 50 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