NHibernate Linq Startwith生成外连接

时间:2015-10-20 21:19:51

标签: nhibernate fluent-nhibernate inner-join outer-join

我有两个通过一对多关系连接的实体。 像这个例子:

public class Category
{
    public int Id {get; set; }
    public string Name {get; set;}
}

public class Product 
{
    public int Id {get; set;}
    public string Name {get; set;}
    public Category ProductCategory {get; set;}
}

使用流畅的nhibernate进行映射

public class CategoryMap : ClassMap<Category>
{
....
}

public Class ProductMap: ClassMap<Product>
{
...
References(x => x.ProductCategory).Column("CategoryId");
}

当我使用linq到NHibernate搜索where子句并且相等时,它会在产品和类别之间生成内部联接

所以这个

var query = session.Query<Product>()
     .Where (x => x.ProductCategory.Name == "abc");

这将生成内连接

但是当我像这样开始搜索时

var query = session.Query<Product>()
     .Where (x => x.ProductCategory.Name.StartWith("abc"));

这将在两者之间产生外连接。

为什么,以及如何强制生成内连接?

1 个答案:

答案 0 :(得分:0)

您可以使用QueryOver来完成,这是在NHibernate中创建查询的另一种方法。在这种情况下,您可以根据需要指定连接类型。

Category category = null;

var result = session.QueryOver<Product>()
                    .JoinAlias(x => x.ProductCategory, () => category, JoinType.InnerJoin)
                    .Where(Restrictions.Like(Projections.Property(() => category.Name), "abc%", MatchMode.Start))
                    .List();

另一方面,查询是更详细的代码,您必须使用linq指定许多避免使用的内容。

相关问题