Search code examples
nhibernatehqlcastle-activerecord

nhibernate hql with named parameter


I have implemented a search function using Castel Active Record. I thought the code is simple enough but I kept getting

NHibernate.QueryParameterException : could not locate named parameter [searchKeyWords]

errors. Can someone tell me what went wrong? Thanks a million.

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

Solution

  • Why do not u pass the % character with parameter?

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