Pergunta

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'
Foi útil?

Solução

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

Outras dicas

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();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top