Home > OS >  how to compare value in one column with other column in datatable C#?
how to compare value in one column with other column in datatable C#?

Time:11-29

I want to compare all the values in column B with the values in column A using the < operator, then display the results in a label..

i got this code which counts how many data have value < 0 in column A. and displays the result in label.

        int Col_A = Dt.AsEnumerable().Where(x => x.Field<decimal>("Column_A") < 0).Count();
        lbCount.Text = numberOfRecords2.ToString();

what i want is "Column_B < Column_A = result".
Thank you :)

CodePudding user response:

@Philipe As discussed in comments you could try x => x.Field<decimal>("Column_B") < x.Field<decimal>("Column_A").

You just need to compare the respective column in place of the hardcoded value 0

CodePudding user response:

if you want count of the rows where value of Column_A is less than Column_B :

 Dt.AsEnumerable().Where(x => int.Parse(x["Column_A"].ToString()) < int.Parse(x["Column_B"].ToString())).Count();
  • Related