c# - Datatable select value where field type is unknown or at run time -


i have simple linq query

linqdt.asenumerable()       .where(w => (w.field<string>(options.selectedcolumn)).contains(options.value))       .copytodatatable(); 

this return me result if selectedcolumn type of string (a.field). problem option.selectedcolumn of multiple types. of byte, date, guid etc etc , don't know these types until run time.

i wondering if there way can pass variable w.field , variable contains datatype based on selectedcolumn? if no, other possibilities?

any highly appreciated

since contains applies string values, use if statement check string values, , use non-generic column accessor , equals other columns:

datatable result; if(linqdt.columns[options.selectedcolumn].datatype == typeof(string))      result = linqdt.asenumerable()                    .where(w => (w.field<string>(options.selectedcolumn))                                  .contains(options.value))                    .copytodatatable(); else     result = linqdt.asenumerable()                    .where(w => (w[options.selectedcolumn]).equals(options.value))                    .copytodatatable(); 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -