[英]NHibernate QueryOver and Collection Filtering
Simple example of my class: 我班上的简单例子:
public class Post
{
public IEnumerable<Tag> Tags { get; set; }
}
User checks few interested tags for filtering Post list. 用户检查了一些感兴趣的标签以过滤帖子列表。
I need to filter all post by selected tags like: 我需要按所选标签过滤所有帖子,例如:
Session.QueryOver<Post>()
.WhereRestrictionOn(x => x.Tags)
.IsIn(criterion.InterestedTags.ToList())
.List<Post>();
Exception: NHibernate.QueryException: Cannot use collections with InExpression
异常:
NHibernate.QueryException: Cannot use collections with InExpression
Actually, I should show post if one of its tag contains in InterestedTags. 实际上,如果其中一个标签包含在InterestedTags中,则应该显示post。
UPD UPD
Works for me: 为我工作:
Session.QueryOver<Post>()
.JoinAlias(p => p.Tags, () => tag)
.WhereRestrictionOn(() => tag.Id)
.IsIn(criterion.InterestedTags.Select(x => x.Id).ToArray())
.List<Post>();
You have to use alias to make restrictions on one-to-many
part 您必须使用别名对
one-to-many
零件进行限制
Try following code snippet: 请尝试以下代码段:
Tag tag = null;
Session.QueryOver<Post>()
.JoinAlias(p => p.Tags, () => tag)
.WhereRestrictionOn(() => tag.Id)
.IsIn(criterion.InterestedTags.ToList()) //*
.List<Post>();
*Assuming that InterestedTags
is collection of identifiers. *假设
InterestedTags
是标识符的集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.