c# - linq as dataset issue with column names -


how can avoid using magic strings in linq query against datatable?

this works:

 public ienumerable getdisplaynames() {     ienumerable nameqry =         row in displaytable.asenumerable()         select row.field("display");      return nameqry; } 

but fails "specified cast not valid.":

 public ienumerable getdisplaynames() {     string disp = mydictionary["d"];      ienumerable nameqry =         row in displaytable.asenumerable()         select row.field(disp);      return nameqry; } 

my preference use local string (or direct reference off of mydictionary) instead of hard coding strings in place. want use string disp instead of phrase "display" in query.

try using

static ienumerable<datarow> gettherows(datatable dt, string name) {     return dt.asenumerable().where(r => r.field<string>("yourcolumn") == name); } 

or whatever correct data type in given scenario. need specify type when using field<t> method.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -