简体   繁体   中英

How can i use SQL inner join statement as query in LINQ

SELECT
    Id,
    No,
    NetAmount,
    PaidAmount,
    NetAmount - PaidAmount AS AmountToBePayed
FROM
    (
        SELECT
            m.Id,
            m.No,
            SUM(NetAmount) as NetAmount,
            (
                SELECT
                    COALESCE( SUM( PaidAmount ), 0 )
                FROM
                    A_Account_Payable_Payment_Invoice
            ) as PaidAmount
        FROM
            A_Account_Payable_Invoice_M m
            INNER JOIN A_Account_Payable_Invoice_Item d ON
                m.Id = d.A_Account_Payable_Invoice
        GROUP BY
            m.Id,
            m.No
    ) a

How can i use this query directly in LINQ C#

As you mentioned on comment section you don't want to translate SQL to LINQ, you just want to directly run this script.

To do so first you need to create class that will match with your Script like:

 [DataContract()]
 public class PaymentInvoiceResult
 {
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public int No { get; set; }

    [DataMember]
    public int NetAmount { get; set; }

    [DataMember]
    public int PaidAmount { get; set; }

    [DataMember]
    public int AmountToBePayed { get; set; }
 }

Then you need to use SqlQuery as following:

List<PaymentInvoiceResult> paymentInvoiceResultList = db.Database.SqlQuery<PaymentInvoiceResult>("Your Script").ToList(); 

You can simply use:

var results = db.ExecuteQuery(typeof(DTO), "sql query ");

Your dto should have members:

Id,
    No,
    NetAmount,
    PaidAmount,
    NetAmount - PaidAmount AS AmountToBePayed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM