-4

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}'")
juunas
  • 54,244
  • 13
  • 113
  • 149
Judy K
  • 31
  • 2
  • You need to give a lot more context here. What is the structure of the table ActiveMedications? What are those dup.DDI and strDDI ? – Steve Feb 20 '17 at 17:57
  • What's your original query to get the datatable, or at least the structure of the datatable? Also [Read this](http://stackoverflow.com/help/how-to-ask). – Andrew Feb 20 '17 at 19:00
  • table has the column strDDI defined as a string and the param I am using to match it is an INTEGER- that's the problem – Judy K Feb 21 '17 at 18:34
  • this works, but its embarrassing: string strDDI = DDI.ToString(); var activeMedDup = ActiveMedications.Select($"DDI = '{strDDI}'").FirstOrDefault(); if (activeMedDup == null) { strDDI = "0" + DDI.ToString(); activeMedDup = ActiveMedications.Select($"DDI = '{strDDI}'").FirstOrDefault(); } – Judy K Feb 21 '17 at 19:20
  • I found a similar problem resolved here: https://blogs.msdn.microsoft.com/spike/2009/05/11/how-use-convert-in-a-datatable-select/ but a co-worker helped me re-write differently using LINQ – Judy K Feb 21 '17 at 20:33

1 Answers1

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