Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
        }
like image 905
Wei Ma Avatar asked Dec 12 '09 01:12

Wei Ma


1 Answers

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();
like image 183
nayakam Avatar answered Sep 17 '22 14:09

nayakam