Reputation: 845
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'
Upvotes: 0
Views: 118
Reputation: 845
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();
}
Upvotes: 0
Reputation: 62248
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.
Upvotes: 3
Reputation: 58
why dont you make a select with linq an then iterate over the list and update the variable??
Upvotes: 0