c# - 'Unable to cast object of type' in linq query -
i have following query using datatable give error: "unable cast object of type 'system.dbnull' type 'system.string'"
var speclist = (from r in dt.asenumerable() (decimal)r["stdyear"] == 0 && (string)r["speclization"] != null select r["speclization"]).distinct().tolist(); speclization may be:
- eng
- null
- math
how can solve problem? guide me.
for r["speclization"] ,it can null,but before check null or not ,you convert string first, excetption throw: unable cast object of type 'system.dbnull' type 'system.string'
var speclist = (from r in dt.asenumerable() r.field<decimal>("stdyear") == 0 && r.field<string?>("speclization").hasvalue select r.field<string?>("speclization").value).distinct().tolist();
Comments
Post a Comment