我有这些课程:
public class Document
{
public Document()
{
Descriptions = new List<Descriptions>();
}
public virtual int Id { get; set; }
public virtual IList<DocumentDescription> Descriptions { get; set; }
}
public class DocumentDescription
{
public virtual int DocumentId { get; set; }
public virtual int LanguageId { get; set; }
}
和映射:
public DocumentMap()
{
Id(x => x.Id);
HasMany(x => x.Descriptions).KeyColumn("DocumentId");
}
public DocumentDescriptionMap()
{
CompositeId()
.KeyProperty(x => x.DocumentId)
.KeyProperty(x => x.LanguageId);
}
我的查询是:
var query = Session.QueryOver<Document>().Where(x => x.Id.IsIn(documentIds)).List();
我需要对解决方案进行查询,以便用少量语言限制DocumentDescriptions,我将获得运行时间。我不想获得一个文档的所有DocumentDescriptions(只有少数)。是否可以为子集合设置过滤器/限制?
答案 0 :(得分:0)
所以,我找到了如何在我的查询中添加额外的join子句:
DocumentDescription dd = null;
ICriterion criterion = Restrictions.On<DocumentDescription>(x => x.LanguageId).IsIn(languageIds.ToArray());
var query = Session.QueryOver<Document>().Where(x => x.Id.IsIn(documentIds));
query.Left.JoinQueryOver(x => x.Descriptions, () => dd, criterion);
SQL:
SELECT * FROM tDocument
LEFT OUTER JOIN tDocumentDescription ON tDocumentDescription.DocumentId = tDocument.Id AND tDocumentDescription.LanguageId IN (@languageIds)
WHERE tDocument.Id IN (@documentIds)