将SQL转换为LINQ所需的帮助

时间:2016-05-21 11:06:44

标签: c# sql sql-server entity-framework linq

我正在尝试将以下SQL转换为LINQ,并且需要一些有关paid_amount和paid_vat的嵌套select子句以及where子句中的select的帮助。忽略索引的东西。

这是SQL

SELECT
   tramps.gl_transaction.debtor_uri,
   tramps.debtor.account_no,
   tramps.gl_transaction.uri as transaction_uri,
   tramps.gl_transaction.base_currency_amount,
   tramps.gl_transaction.base_currency_vat_amount,

   (select IsNull(sum(gltr.base_currency_amount), 0.00)
     from tramps.gl_transaction gltr
     where gltr.sibling_uri    = tramps.gl_transaction.uri) * (-1) as Paid_Amount ,

   (select IsNull(sum(gltr.base_currency_vat_amount), 0.00)
     from tramps.gl_transaction gltr
     where gltr.sibling_uri = tramps.gl_transaction.uri) * (-1) as Paid_Vat ,


   tramps.account.property_ref,
   tramps.account.sub_ledger_code,
    tramps.gl_transaction.transaction_type_code,
    tramps.gl_transaction.transaction_description,
   tramps.gl_transaction.effective_date

FROM tramps.account, tramps.chart WITH (INDEX (PK_CHART)), tramps.gl_transaction WITH (INDEX (glt_debtor_gen)) ,tramps.debtor, tramps.receivables_register , tramps.bank_account
 WHERE tramps.chart.code = tramps.account.chart_code
 AND tramps.gl_transaction.debtor_uri       = tramps.debtor.uri
 AND tramps.gl_transaction.account_uri      = tramps.account.uri
 AND tramps.receivables_register.uri            =       tramps.gl_transaction.receivables_register_uri
  AND tramps.receivables_register.bank_account_uri   = tramps.bank_account.uri
  AND tramps.chart.control_account           = 'Debtor'
  AND tramps.gl_transaction.status           = 'L'
  AND tramps.gl_transaction.process_status       = 'Released'

  AND (   (tramps.gl_transaction.generated = 'C' AND tramps.gl_transaction.sibling_uri IS NULL  AND tramps.gl_transaction.written_off <> 'Y')
     OR (tramps.gl_transaction.generated = 'N' AND tramps.gl_transaction.sibling_uri IS NULL) )
     AND (tramps.gl_transaction.base_currency_amount +
   (select IsNull(sum(gltr.base_currency_amount), 0.00)
    from tramps.gl_transaction gltr
    where gltr.sibling_uri = tramps.gl_transaction.uri) <> 0.00 )

这是迄今为止我设法创建的LINQ,但我正在努力使用嵌套选择,sum和where子句

 from acc in Accounts
        join chart in Charts on acc.Chart_code equals chart.Code
        join gltrans in Gl_transactions on acc.Uri equals gltrans.Account_uri  
        join debt in Debtors on gltrans.Debtor_uri equals debt.Uri    
        join recreg in Receivables_registers on gltrans.Receivables_register_uri equals recreg.Uri    
        join bankacc in Bank_accounts on recreg.Bank_account_uri equals bankacc.Uri     

        let paidcurrency = from gltrns in Gl_transactions
                    where gltrns.Sibling_uri == gltrans.Uri
                    select gltrns.Base_currency_amount

        where 
            chart.Control_account  == "Debtor"
        &&  gltrans.Status         == "L"
        &&  gltrans.Process_status == "Released"
        &&  acc.Property_ref == 102979

        && ((gltrans.Generated == "C" && gltrans.Sibling_uri == null  && gltrans.Written_off != "Y")
            || (gltrans.Generated == "N" && gltrans.Sibling_uri == null) )

        select new
        {
            gltrans.Debtor_uri,
            debt.Account_no,
            gltrans.Uri,
            gltrans.Base_currency_amount,
            gltrans.Base_currency_vat_amount,

            er = (decimal?)paidcurrency.Base_currency_vat_amount.Sum() ?? 0,

            acc.Property_ref,
            acc.Sub_ledger_code,
            gltrans.Transaction_type_code,
            gltrans.Transaction_description,
            gltrans.Effective_date
        }

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助。我混合了一些流利的语法来帮助翻译Sum函数。如果其中一些翻译有帮助,请告诉我,因为我无法在没有您的架构和数据的情况下直接检查它。

var result =
    from acc in Accounts
    join chart in Charts on acc.Chart_code equals chart.Code
    join gltrans in Gl_transactions on acc.Uri equals gltrans.Account_uri
    join debt in Debtors on gltrans.Debtor_uri equals debt.Uri
    join recreg in Receivables_registers on gltrans.Receivables_register_uri equals recreg.Uri
    join bankacc in Bank_accounts on recreg.Bank_account_uri equals bankacc.Uri

    // query expression
    //let paidcurrency = from gltrns in Gl_transactions
    //                   where gltrns.Sibling_uri == gltrans.Uri
    //                   select gltrns.Base_currency_amount

    // change to fluent..
    let paid_Amount = gltrans.Where(g => g.Sibling_uri == g.Uri)
                    .Select(g => g.Base_currency_amount * -1 ?? 0.00M).Sum()

    // same for Vat..
    let paid_Vat = gltrans.Where(g => g.Sibling_uri == g.Uri)
                    .Select(g => g.Base_currency_vat_amount * -1 ?? 0.00M).Sum()

    // added another 'let' to use in the where clause..
    let base_Currency = gltrans.Where(g => g.Sibling_uri == g.Uri)
                        .Select(g => g.Base_currenct_amount ?? 0.00M).Sum()

    where
        chart.Control_account == "Debtor"
        && gltrans.Status == "L"
        && gltrans.Process_status == "Released"
        && acc.Property_ref == 102979

        && (
            (
                gltrans.Generated == "C" && 
                gltrans.Sibling_uri == null && 
                gltrans.Written_off != "Y"
            )
            || 
            (
                gltrans.Generated == "N" && 
                gltrans.Sibling_uri == null
            )
            )

        && gltrans.Base_currency_amount + base_Currency != 0.00M

    select new
    {
        gltrans.Debtor_uri,
        debt.Account_no,
        gltrans.Uri,
        gltrans.Base_currency_amount,
        gltrans.Base_currency_vat_amount,

        //er = (decimal?)paidcurrency.Base_currency_vat_amount.Sum() ?? 0,
        paid_Amount,
        paid_Vat,

        acc.Property_ref,
        acc.Sub_ledger_code,
        gltrans.Transaction_type_code,
        gltrans.Transaction_description,
        gltrans.Effective_date
    };