How to solve the problem that the specified conversion is invalid in the LINQ operation DataTable
This article mainly explains the "LINQ operation DataTable specified conversion invalid problem how to solve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "LINQ operation DataTable specified conversion invalid problem how to solve" it!
Code
Public static IEnumerable getEnumerable () {DataTable dt = getDatatable () Try {var query = from q in dt.AsEnumerable () where q.Field ("IPLocation") = "Pure Network June 20, 2008 IP data" select new {IPid = q.Field ("IPid"), IPFrom = q.Field ("IPFrom"), IPTo = q.Field ("IPTo"), IPCity = q.Field ("IPCity"), IPToNumber = q.Field ("IPToNumber") IPFromNumber = q.Field ("IPFromNumber")} Return query;} catch {return null;}}
Then when I bind a data source in *, I always report System.InvalidCastException: the specified transformation is invalid, no matter using the automatically generated column of Gridview or the manual column added by Reapter, there is a problem. * I found that the generated result set contains exceptions, that is, when taking the result set, you should use the following words carefully:
IPid = q.Field ("IPid"), IPFrom = q.Field ("IPFrom"), IPTo = q.Field ("IPTo"), IPCity = q.Field ("IPCity"), IPToNumber = q.Field ("IPToNumber"), IPFromNumber = q.Field ("IPFromNumber")
I later tried another way to write it:
IPid = Q ["IPid"] .ToString (), IPFrom = Q ["IPFrom"] .ToString (), IPTo = Q ["IPTo"] .ToString (), IPLocation = Q ["IPLocation"] .ToString (), IPCity = Q ["IPCity"] .ToString (), IPToNumber = Q ["IPToNumber"] .ToString (), IPFromNumber = Q ["IPFromNumber"] .ToString ()
At the beginning, I still reported System.InvalidCastException: the specified conversion is invalid. I debugged for some time, but there was still a problem. I wondered if it was the problem of data binding control. Later, I replaced the original Gridview auto-generated column with a manual input column:
Code
ID IPFrom IPTo IPLocation City IPToNumber IPFromNumber
Later, the problem was solved, and now I don't know the cause of the problem, why not support automatic generation of columns (waiting for the master to help solve it)?
Now that you know what the problem is, you can freely process the data in DataTable, such as the following simple example:
Code
Public static IEnumerable getEnumerable () {DataTable dt = getDatatable () Try {var query = from q in dt.AsEnumerable () orderby long.Parse (Q ["IPid"] .ToString () descending where q ["IPid"] .ToString () = "345058" select new {IPid = Q ["IPid"] .ToString (), IPFrom = Q ["IPFrom"] .ToString (), IPTo = Q ["IPTo"] .ToString (), IPLocation = Q ["IPLocation"] .ToString () IPCity = Q ["IPCity"] .ToString (), IPToNumber = Q ["IPToNumber"] .ToString (), IPFromNumber = Q ["IPFromNumber"] .ToString ()} Return query; / / List list = new List (); / / foreach (var q in query) / / {/ / IPInfo model = new IPInfo (); / / model.IPCity = q.IPCITY; / / model.IPFrom = q.IPFromNumber; / / model.IPFromNumber = q.IPFromNumber; / / model.IPid = long.Parse (q.IPid); / / model.IPLocation = q.IPLocation; / / model.IPTo = q.IPTo / / model.IPToNumber = q.IPToNumber; / / list.Add (model); / /} / / return list;} catch {return null }} Thank you for your reading, the above is the content of "how to solve the specified conversion invalid problem in LINQ operation DataTable". After the study of this article, I believe you have a deeper understanding of how to solve the specified conversion invalid problem in LINQ operation DataTable, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!