【问题标题】:Find entities that match all list elements in a list查找与列表中所有列表元素匹配的实体
【发布时间】:2013-09-18 12:01:17
【问题描述】:

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

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

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

下面的 sn-p 返回所有匹配至少一个标签的产品:

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

我怎样才能获得包含具有所有标签名称的产品的列表?

【问题讨论】:

    标签: nhibernate queryover


    【解决方案1】:

    假设您的 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-12-06
      • 2020-07-31
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多