How to subtract two columns

filter_Status1.AsEnumerable().Select(Function(row) row.SetField(Of Double)(“(B-P) Amt”, If(CDbl(row(“Bal Amt”)) <> 0 AndAlso CDbl(row(“Pat Amt”)) <> 0, CDbl(row(“Bal Amt”)) - CDbl(row(“Pat Amt”)), 0))).CopyToDataTable()
correct this

.

Hi @Saurabh_Kumar1

You want to subtract the two columns and where you want to write the subtraction result in the excel file.

If your excel doesn’t have the confidential data then share the input data and expected output.

Hope you understand!!

Bal Amt -$20.00
Pat Amt -$10.00
(B-P)Amt = ?
please suggest me in linq code

if u got my point suggest me

Hi @Saurabh_Kumar1

Try this

Code:

For Each row As DataRow In dt.Rows
    Dim amount1 As Double = 0
    Dim amount2 As Double = 0

    If Double.TryParse(row("Amount1").ToString().Replace("$", "").Replace(",", "").Trim(), amount1) = False Then
        amount1 = 0
    End If

    If Double.TryParse(row("Amount2").ToString().Replace("$", "").Replace(",", "").Trim(), amount2) = False Then
        amount2 = 0
    End If

    row("Difference") = amount1 - amount2
Next

Output:

image

Regards,

Hi @Saurabh_Kumar1

I got your point, use the below LINQ Expression to achieve it.
→ Use the Read range workbook activity to read the excel and store in a datatable called Datatable.
→ Then use the assign activity and give the below expression,

- Assign -> ResultDatatable = (From row In Datatable.AsEnumerable()
                               Let BalAmt As Double = CDbl(row("Bal Amt").ToString().Replace("$", "").Trim())
                               Let PatAmt As Double = CDbl(row("Pat Amt").ToString().Replace("$", "").Trim())
                               Let BPAmt As Double = If(BalAmt = 0 AndAlso PatAmt = 0, 0, BalAmt - PatAmt)
                               Select Datatable.Clone.LoadDataRow(New Object() {row("Bal Amt"), row("Pat Amt"), BPAmt.ToString()}, False)).CopyToDataTable()

→ Then use the write range workbook activity to write the output data to excel.

Check the below workflow for your better understanding,

Hope it helps!!