I am converting a datatable to dictionary using below code:
Dictionary<string, decimal> dict = dt.AsEnumerable()
.Select(dr => new { Key = dr["key_col"], Value = dr["value_col"] })
.ToDictionary(kvp => (string)kvp.Key, kvp => (decimal)kvp.Value);
but it throws me an 'input string not in correct format' exception. I am pretty sure its because of some null rows present in the datatable. how can I filter out those null rows from this code?
decimalcolumns and not only string columns? Look atdt.Columns. If it only contains strings you can't cast it (or use myField<decimal>approach). You have to parse it, f.e.decimal.Parse(dr.Field<string>("value_col"))dt.Columns["value_col"].DataTypereturn?decimal.Parse()it gave me an error. so I had to useConvert.ToDecimal(str)in the end. it worked.