c# - 如何为给定的 sql 查询创建条件查询

标签 c# hibernate nhibernate fluent-nhibernate nhibernate-criteria

我正在为这个等效的 sql 查询创建一个 ICriteria 查询。

SELECT fCustomerID,
       ISNULL(
                (SELECT SUM(payinv.fAmount) AS Expr1
                 FROM dbo.tARPayment AS pay
                 INNER JOIN dbo.tARPaymentInvoice AS payinv ON pay.fPaymentID = payinv.fPaymentID
                 INNER JOIN dbo.tARInvoice AS inv ON payinv.fInvoiceID = inv.fARInvoiceID
                 WHERE (pay.fIsPosted = CASE pay.fPaymentType WHEN 'CM' THEN 0 WHEN 'EPD' THEN 0 ELSE 1 END)
                   AND (inv.fCustomerID <> dbo.tARCustomer.fCustomerID)
                   AND (pay.fCustomerID = dbo.tARCustomer.fCustomerID)), 0)
FROM dbo.tARCustomer
GROUP BY fCustomerID

但无论如何我都不知道如何生成等效的 nhibernate ICriteria 查询。

这是支付类

public partial class tARPayment 
{
    #region Constructor

    /// <summary>
    /// Initializes a new instance of the <see cref="tARPayment"/> class.
    /// </summary>
    public tARPayment()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="tARPayment"/> class.
    /// </summary>
    /// <param name="fPaymentID">The fPaymentID of guid type.</param>
    public tARPayment(System.Guid fPaymentID)
    {
        this.ID = fPaymentID;
    }
    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets payment id.
    /// </summary> 
    public virtual System.Guid fPaymentID { get; set; }

    /// <summary>
    /// Gets or sets fCustomerID.
    /// </summary> 
    public virtual System.Guid fCustomerID { get; set; }

    /// <summary>
    /// Gets or sets check number.
    /// </summary> 
    public virtual string fCheckNumber { get; set; }

    /// <summary>
    /// Gets or sets amount.
    /// </summary> 
    public virtual decimal fAmount { get; set; }       

    /// <summary>
    /// Gets or sets customer detail.
    /// </summary> 
    public virtual tARCustomer Customer { get; set; }

    public virtual IList<tARPaymentInvoice> PaymentInvoices { get; set; }        

    #endregion

    #region Methods
    /// <summary>
    /// partial class for payment.
    /// </summary>
    /// <returns>The method get code.</returns>
    public override int GetHashCode()
    {
        return ID.GetHashCode();
    }
    #endregion
}

这是发票类

public partial class tARInvoice 
{
    #region Constructor
    /// <summary>
    /// Initializes a new instance of the <see cref="tARInvoice"/> class.
    /// </summary>
    public tARInvoice()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="tARInvoice"/> class.
    /// </summary>
    /// <param name="fARInvoiceID">The fARInvoiceID.</param>
    public tARInvoice(System.Guid fARInvoiceID)
    {
        this.ID = fARInvoiceID;
    }

    #endregion

    #region Properties
    /// <summary>
    /// Gets or sets fARInvoiceID.
    /// </summary> 
    public virtual Guid fARInvoiceID { get; set; }

    /// <summary>
    /// Gets or sets fCustomerID.
    /// </summary> 
    public virtual Guid fCustomerID { get; set; }


    /// <summary>
    /// Gets or sets Delivery Method.
    /// </summary> 
    public virtual string fDeliveryMethod { get; set; }

    /// <summary>
    /// Gets or sets Invoice Number.
    /// </summary> 
    public virtual int? fARInvoiceNumber { get; set; }



    public virtual tARCustomer Customer { get; set; }

    public virtual IList<tARPaymentInvoice> PaymentInvoices { get; set; }        

    #endregion

    #region Methods
    /// <summary>
    /// retrieve Hash Code.
    /// </summary>
    /// <returns>The method get code.</returns>
    public override int GetHashCode()
    {
        return ID.GetHashCode();
    }
    #endregion
}

这是一个付款发票类。

public partial class tARPaymentInvoice 
{
    #region Constructor
    /// <summary>
    /// Initializes a new instance of the <see cref="tARPaymentInvoice"/> class.
    /// </summary>
    public tARPaymentInvoice()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="tARPaymentInvoice"/> class.
    /// </summary>
    /// <param name="fPaymentInvoiceID">The Invoice ID.</param>   
    public tARPaymentInvoice(System.Guid fPaymentInvoiceID)
    {
        this.ID = fPaymentInvoiceID;
    }
    #endregion

    #region Properties
    /// <summary>
    /// Gets or sets fPaymentInvoiceID.
    /// </summary>
    public virtual System.Guid fPaymentInvoiceID { get; set; }

    /// <summary>
    /// Gets or sets fPaymentID.
    /// </summary>
    public virtual System.Guid fPaymentID { get; set; }

    /// <summary>
    /// Gets or sets fInvoiceID.
    /// </summary>
    public virtual System.Guid fInvoiceID { get; set; }           


    /// <summary>
    /// Gets or sets tARPayment.
    /// </summary>
    public virtual tARPayment Payment { get; set; }

    /// <summary>
    /// Gets or sets tARInvoice.
    /// </summary>
    public virtual tARInvoice Invoice { get; set; }

    #endregion

    #region Methods
    /// <summary>
    /// get hash codes.
    /// </summary>        
    /// <returns>The hash code.</returns>
    public override int GetHashCode()
    {
        return ID.GetHashCode();
    }
    #endregion
}

最佳答案

与其将上述查询转换为 LINQ 或 HQL,我建议将查询转换为 View ,然后使用 NHibernate 查询该 View 。

SQL

CREATE VIEW vCustomerAmount AS
SELECT fCustomerID,
       ISNULL(
                (SELECT SUM(payinv.fAmount) AS Expr1
                 FROM dbo.tARPayment AS pay
                 INNER JOIN dbo.tARPaymentInvoice AS payinv ON pay.fPaymentID = payinv.fPaymentID
                 INNER JOIN dbo.tARInvoice AS inv ON payinv.fInvoiceID = inv.fARInvoiceID
                 WHERE (pay.fIsPosted = CASE pay.fPaymentType WHEN 'CM' THEN 0 WHEN 'EPD' THEN 0 ELSE 1 END)
                   AND (inv.fCustomerID <> dbo.tARCustomer.fCustomerID)
                   AND (pay.fCustomerID = dbo.tARCustomer.fCustomerID)), 0) [Amount]
FROM dbo.tARCustomer
GROUP BY fCustomerID

C# DTO

public class CustomerAmount
{
    public int fCustomerID { get; set; }
    public decimal Amount { get; set; }
}

查询

List<CustomerAmount> customerAmounts = session.Query<CustomerAmount>().ToList();

关于c# - 如何为给定的 sql 查询创建条件查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13970214/

相关文章:

Hibernate 查询缓存 - 对于不在二级缓存中的对象 - 有风险吗?有用?不好的做法?

nhibernate - 在 NHibernate 中,如何找到知道其类型的持久性 POCO 类的 id 属性的名称?

nHibernate Collections 问题(检查您的映射文件中的属性类型不匹配)

sql-server-2008 - NHibernate : Store VARBINARY to MAX

c# - 为什么我的 100,000 英镑被转换为 100 而不是 100000,这可能是正则表达式的问题?

c# - 以十六进制值表示的 int 枚举

hibernate - 使用 hibernate 进行缓存

java - 从 hibernate session 中删除对象?

c# - Forms.OpenFileDialog() 和 Forms.FolderBrowserDialog() 初始路径行为

c# - Windows 性能计数器从 PerfMon 中消失