문제

I have the following SQL statement to be converted into LINQ in C#. The @amount variable will be a C# variable.

DECLARE @amount DECIMAL(20,2)
SET @amount = 120.30

UPDATE INVOICES SET AmtPaid = AmtPaid + @amount WHERE InvoiceNum = 'AC0000034550'
도움이 되었습니까?

해결책

why dont you make a select with linq an then iterate over the list and update the variable??

다른 팁

You cannot UPDATE with LINQ.

LINQ is a query engine, suitable for SELECT (if we talk in terms of raw SQL).

Plus, if you already have good working SQL code, it does not make any sense to convert it into LINQ; SQL is faster, and works.

LINQ does not do updates,so you cannot convert the SQL to LINQ

I did as follows. Not sure whether it is correct or not:

using (var dataContext = GetDataContext())
{
    var invoiceData = from i in dataContext.Invoices
                      where i.InvoiceNumber == paymentData.InvoiceNumber
                      select i;
    foreach(var invoice in invoiceData)
    {
        decimal outPut;
        if (Decimal.TryParse(paymentData.AmountPaid, out outPut))
        {
            invoice.AmtPaid = invoice.AmtPaid + Convert.ToDecimal(paymentData.AmountPaid);
        }
    }
    dataContext.SubmitChanges();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top