C# datable.select with convert fails - why?

51 Views Asked by At

My code is comparing a passed in integer parameter to a datatable with corresponding string column. The real problem is that often the string value is preceded with 1 or more zeros. so I end up comparing string "000123" to integer 123 and they do not match.

So I would like to convert the string to an integer and compare as integers. I tried this but it doesn't work (I get error "Cannot find column [int]"):

var activeMedDup = ActiveMedications?.Select($"convert(int,strDDI) = '{dup.DDI}'")
1

There are 1 best solutions below

0
Judy K On
  var activeMedDup = ActiveMedications.Rows
                                        .OfType<DataRow>()
                                        .FirstOrDefault(x => Convert.ToInt32(x.Field<string>("DDI")) == ddi);