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



我想选择属性 Items (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

从技术上讲,如果您执行内部联接,则不会收到任何没有任何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

最新更新