NHibernate QueryOver并选择属性为ILIST且列表计数> = 1的行

时间:2019-06-18 20:45:06

标签: nhibernate queryover

我想选择属性IList本身具有行的行。在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无法识别

1 个答案:

答案 0 :(得分:0)

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

因此,作为一个非常简单的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