带有命名参数的nhibernate hql

时间:2009-12-12 01:33:30

标签: nhibernate hql castle-activerecord

我使用Castel Active Record实现了搜索功能。我认为代码很简单,但我一直在

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();
        }

1 个答案:

答案 0 :(得分:15)

为什么不用参数传递%字符?

   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();