流利的Nhibernate获得孩子数大于n的不同父母

时间:2015-10-06 07:28:59

标签: c# fluent-nhibernate fluent-nhibernate-mapping

以下是我的实体。一个ContractBill可以有多个SubscriberInvoices。

public class ContractBill
{
    public virtual Int32 ContractID { get; set; }
    public virtual Int32 CustomerID { get; set; }
    public virtual string ContractStatus { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual IList<SubscriberInvoice> InvoiceList { get; set; }
    public ContractBill() { }
}

public class ContractBillMap : ClassMap<ContractBill>
{
    public ContractBillMap()
    {
        Table("GT_CONTRACT");
        Id(x => x.ContractID, "GF_CONT_ID").GeneratedBy.Increment();
        Map(x => x.CustomerID, "GF_CUST_ID");
        Map(x => x.ContractStatus, "GF_CONTRACT_STATUS");
        HasMany(x => x.InvoiceList).KeyColumn("GF_CONT_ID").Not.LazyLoad();
     }
}

public class SubscriberInvoice : Object
{
    virtual public String InvoiceID { get; set; }
    virtual public Int32 ContractID { get; set; }
    virtual public Int32 CustomerID { get; set; }
    virtual public String BillPeriod { get; set; }
    virtual public Double Amount { get; set; }
    virtual public Double MinimumAmount { get; set; }
    virtual public DateTime PayByDate { get; set; }
    virtual public String IsPaid { get; set; }
    virtual public Double AmountPaid { get; set; }

    public SubscriberInvoice()
    {
    }
}

public class SubscriberInvoiceMap : ClassMap<SubscriberInvoice>
{
    public SubscriberInvoiceMap()
    {
        Table("GT_BILL");
        CompositeId()
          .KeyProperty(x => x.InvoiceID, "GF_BILL_ID")
          .KeyProperty(x => x.ContractID, "GF_CONT_ID")
          .KeyProperty(x => x.BillPeriod, "GF_BILL_PERIOD")
        Map(x => x.Amount, "GF_AMOUNT");
        Map(x => x.MinimumAmount, "GF_MINIMUM_AMOUNT");
        Map(x => x.PayByDate, "GF_PAY_BY_DATE");
        Map(x => x.IsPaid, "GF_PAID");
        Map(x => x.AmountPaid, "GF_AMOUNT_PAID");
     }

}

//获取具有两个以上非付费订阅者发票的不同BillContract。

ICriteria criteria = session.CreateCriteria(typeof(ContractBill), "cb1");

string aliasName = "invoice";
string prefix = aliasName + ".";
criteria = criteria.CreateAlias("InvoiceList", aliasName);
criteria = criteria.Add(Expression.Eq(prefix + "IsPaid", "N"));

// below detached query will give the count of non paid invoice list
DetachedCriteria crit = DetachedCriteria.For(typeof(ContractBill))
                .CreateAlias("InvoiceList", "InvoiceList")
                .SetProjection(Projections.ProjectionList()
                .Add(Projections.RowCount()))
         .Add(Property.ForName("ContractID").EqProperty("cb1.ContractID"));

 //Non paid invoice Count = 2           
 criteria = criteria.Add(Restrictions.Gt(Projections.SubQuery(crit), 2));

//Not giving proper reasult when being used with paging
//criteria = Criteria.SetResultTransformer(Transformers.DistinctRootEntity);

 criteria =criteria.SetFirstResult(0).SetMaxResults(10);

 //Tried with below detached query, but not getting distinct record
 DetachedCriteria crit1 = DetachedCriteria.For(typeof(ContractBill))
           .Add(Restrictions.EqProperty ("ContractID", "cb1.ContractID"))
            .CreateCriteria("InvoiceList")
           .SetProjection(Projections.Distinct(Projections.Id()));

 criteria = criteria.Add(Subqueries.PropertyIn("cb1.ContractID", crit1));
  IList<ContractBill> matchingObjects = criteria.List<ContractBill>();
  1. 上面的代码没有给出明显的结果。请建议如何获取不同的ContractBill记录。

  2. 为了显示包含分页的记录,我还需要获得上述条件的总记录数。

0 个答案:

没有答案