1

我有以下(简化的)多对多关系:

public class Tag
{
    public string Name { get; set; }
    public IList<Product> Products { get; set; }
}

public class Product
{
    public IList<Tag> Tags { get; set; }
}

以下代码段返回与至少一个标签匹配的所有产品:

var searchTags = new[] {"tag1", "tag3"};
Tag tagAlias = null;
var query = _session.QueryOver<Product>()
               .JoinAlias(p => p.Tags, () => tagAlias)
               .WhereRestrictionOn(() => tagAlias.Name).IsIn(searchTags)
               .List();

如何获得包含所有标签名称的产品的列表?

4

1 回答 1

2

假设您的Product班级至少Id有财产,您可能会选择这样的东西。如果有多个Id属性,则必须显式选择所有这些属性。

var searchTags = new[] { "tag1", "tag3" };
Tag tagAlias = null;

Product pr = null, resProduct = null;

var products = 
    session.QueryOver(() => pr)
        .JoinAlias(() => pr.Tags, () => tagAlias)
        .WhereRestrictionOn(() => tagAlias.Name).IsIn(searchTags)
        .SelectList(list => list
                                .SelectGroup(p => p.Id)
                                .SelectCount(p => tagAlias.Name))
        .Where(Restrictions.Eq(Projections.Count<Product>(p => tagAlias.Name),
                                searchTags.Length))
        .SelectList(list => list
                                .SelectGroup(p => p.Id).WithAlias(() => resProduct.Id))
        .TransformUsing(Transformers.AliasToBean<Product>())
        .List();

我敢打赌,答案不那么复杂,只是找不到。希望这无论如何都会有所帮助。

于 2013-09-18T14:37:32.697 回答