【问题标题】:NHibernate QueryOver and selecting rows where a propery is an ILIST and the list count >= 1NHibernate QueryOver 并选择属性为 ILIST 且列表计数 >= 1 的行
【发布时间】:2019-06-18 20:45:06
【问题描述】:

我想选择 IList 属性 Items 本身具有行的行。在 SQL 中,它的简单计数 >= 1,但在 NHibernate 中却让我无法理解。

尝试了很多方法

public class Sale
{
    private IList<Items> _items;

    public Sale()
    {
        _items = new List<Item>();
    }

    public Guid SaleId { get; set; }
    public string SaleNumber { get; set; }
    public string Location { get; set; }
    public DateTime? SaleDateTime { get; set; }

    public IList<Item> Items => _items;
}

public class Item
{
    public Guid ItemId { get; set; }
    public string description { get; set; }
}

        var testdata = _session.QueryOver<Sale>()
            .Where(Restrictions.Ge(
                Projections.Property<Sale>
                (m => m.Items.Count), 1))
            .ReadOnly()
            .ListAsync();

Count 无法识别

【问题讨论】:

    标签: nhibernate queryover


    【解决方案1】:

    从技术上讲,如果您进行了内部连接,您将不会收到任何没有任何 Item 记录的 Sale 记录。

    所以作为一个非常简单的QueryOver,你可以这样做:

    var sales = session.QueryOver<Sale>()
      .Inner.JoinQueryOver(x => x.Items)
      .Select(Projections.RootEntity())
      .TransformUsing(Transformers.DistinctRootEntity)
      .List();
    

    这是生成的 SQL:

    SELECT this_.SaleId as saleid1_1_0_, 
      this_.SaleNumber as salenumber2_1_0_, 
      this_.Location as location3_1_0_, 
      this_.SaleDateTime as saledatetime4_1_0_ 
    FROM Sale this_ 
     inner join Item item1_ on this_.SaleId=item1_.SaleId
    

    【讨论】:

    • 谢谢乔,我确实进行了内部连接,但它为每个项目提供了一行。如果有任何物品,我只想要一排。我什至尝试了一个不同的加入标准。我真的只需要销售行。
    • Joe 我的旧联接没有投影和转换,这似乎消除了根实体 'sales' 的重复行。所以谢谢。我添加了投影和转换,现在我得到了干净的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多