nhibernate hql with named parameter

人走茶凉 提交于 2019-12-10 03:00:44

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/1891901/nhibernate-hql-with-named-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!