-1
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#

Dai
  • 141,631
  • 28
  • 261
  • 374
Ayesha Sheikh
  • 13
  • 1
  • 5

2 Answers2

0

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(); 
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
0

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
Gauravsa
  • 6,330
  • 2
  • 21
  • 30