nhibernate hql with named parameter

Wei Ma picture Wei Ma · Dec 12, 2009 · Viewed 9k times · Source

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

Answer

nayakam picture nayakam · Dec 12, 2009

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