nhibernate或sql语句中的

nhibernate或sql语句中的,nhibernate,Nhibernate,如何在nhibernate中获得sql“或”的等价物 我有以下方法。基本上我想看看关键字是在product.Name还是product.Description中找到的 public ICollection<ProductCategory> FindByCompanyIdAndSearchAndPop(int companyId, string keyword) { var products = _session .CreateCr

如何在nhibernate中获得sql“或”的等价物

我有以下方法。基本上我想看看关键字是在product.Name还是product.Description中找到的

   public ICollection<ProductCategory> FindByCompanyIdAndSearchAndPop(int companyId, string keyword)
    {
        var products = _session
            .CreateCriteria(typeof(ProductCategory))
            .CreateAlias("Product", "product", JoinType.InnerJoin)
            .Add(Restrictions.Eq("CompanyId", companyId))
            .Add(Restrictions.Eq("product.IsPopItem", true))
            .Add(Restrictions.Like("product.Name", keyword, MatchMode.Anywhere))
            .Add(Restrictions.Like("product.Description", keyword, MatchMode.Anywhere))
            .List<ProductCategory>();
        return products;
    }
public ICollection findbycompanyid和searchandpop(int companyId,string关键字)
{
var产品=\u会话
.CreateCriteria(类型(产品类别))
.CreateAlias(“产品”、“产品”、JoinType.InnerJoin)
.Add(Restrictions.Eq(“CompanyId”,CompanyId))
.Add(Restrictions.Eq(“product.IsPopItem”,true))
.Add(限制,如(“product.Name”、关键字、匹配模式、任意位置))
.Add(限制,如(“product.Description”、关键字、匹配模式、任意位置))
.List();
退货产品;
}

我相信你可以这样使用Expression.Or(restriction1,restriction2):

.Add(Expression.Or(Restrictions.Like("product.Name", keyword, 
   MatchMode.Anywhere),Restrictions.Like("product.Description", keyword, 
   MatchMode.Anywhere)))

如果我没记错的话,您也可以嵌套表达式。或者的

我相信您可以使用表达式。或者(restriction1,restriction2)如下:

.Add(Expression.Or(Restrictions.Like("product.Name", keyword, 
   MatchMode.Anywhere),Restrictions.Like("product.Description", keyword, 
   MatchMode.Anywhere)))

如果我没记错的话,您也可以嵌套表达式。或者的
|
运算符因限制而重载,因此您可以编写:

.Add(
    Restrictions.Like("product.Name", keyword, MatchMode.Anywhere) ||
    Restrictions.Like("product.Description", keyword, MatchMode.Anywhere)))

|
运算符因限制而重载,因此您可以编写:

.Add(
    Restrictions.Like("product.Name", keyword, MatchMode.Anywhere) ||
    Restrictions.Like("product.Description", keyword, MatchMode.Anywhere)))

你想要这段代码的SQL等价物吗?你想要这段代码的SQL等价物吗?