Conversion of Field Type to Double

637
2
Jump to solution
04-12-2023 06:20 PM
tzz_12
by
New Contributor III

I am getting the error "System.InvalidCastException:'Unable to cast object of type 'ArcGIS.Core.Data.Field' to type  'System.IConvertible'. I am pretty sure this worked for me before, but I got this error when I applied it to the "Shape_Area" field. If anyone encountered the same problem, how did you resolve it? Thanks!

 

 Field areaField = modelSoilsDef.GetFields().FirstOrDefault(x => x.Name.Contains("Shape_Area"));
                        var Area = Convert.ToDouble(areaField);

 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

First, you don't need use Linq to find field.

Field areaField = modelSoilsDef.FindField("Shape_Area");

To find spatial fields like area or length don't use strings like "Shape_Area". Name of spatial field could depend on database type. Use ArcGIS Pro SDK for these fields:

string areaFieldName = modelSoilsDef.GetAreaField();

 

And the last thing. As I understand you want to read row value of field "Shape_Area". Your code could look like this:

var Area =  Convert.ToDouble(row[modelSoilsDef.GetAreaField()]);

If you want to read more than one row:

string areaFieldName = modelSoilsDef.GetAreaField();

// inside row cursor

var Area =  Convert.ToDouble(row[areaFieldName]);

 

View solution in original post

2 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

First, you don't need use Linq to find field.

Field areaField = modelSoilsDef.FindField("Shape_Area");

To find spatial fields like area or length don't use strings like "Shape_Area". Name of spatial field could depend on database type. Use ArcGIS Pro SDK for these fields:

string areaFieldName = modelSoilsDef.GetAreaField();

 

And the last thing. As I understand you want to read row value of field "Shape_Area". Your code could look like this:

var Area =  Convert.ToDouble(row[modelSoilsDef.GetAreaField()]);

If you want to read more than one row:

string areaFieldName = modelSoilsDef.GetAreaField();

// inside row cursor

var Area =  Convert.ToDouble(row[areaFieldName]);

 

tzz_12
by
New Contributor III

Thank you for your detailed solution. 

 

0 Kudos