错误返回无法强制转换'System.Decimal'类型的对象以键入'System.String'

时间:2016-01-05 07:35:13

标签: c# asp.net .net

在传递数据集表以便转换为列表时返回异常。

//here i am passing dataset tables to FromDataRow   
var list = ClassA.FromDataRow(ds.Tables[1].AsEnumerable());

//here is function implementation
    public class ClassA
    {
        public List<UserDesignation> UserDesigGrid { get; set; }

        public static ClassA FromDataRow(IEnumerable<DataRow> userDesignationDetails, IEnumerable<DataRow> userContactDeails, IEnumerable<DataRow> userCategoryDetails)
        {
            var classA = new ClassA
            {
                UserDesigGrid = userDesignationDetails.Select(r => new UserDesignation
                {
                    USER_ID = (int)r["USER_ID"],
                    DESIGNATION = (string)r["DESIGNATION"],
                    USER_TYPE = (string)r["LOCATION_TYPE"],
                    LOCATION = (string)r["LOCATION_ID_NAME"],
                    EFFECTIVE_FROM = (string)r["WEF_DATE"],
                    EFFECTIVE_TO = (string)r["WET_DATE"],

                }).ToList()           
             };
            return classA;
        }
    }
//property layer
 public class UserDesignation
    {
        public int USER_ID { get; set; }
        public string DESIGNATION { get; set; }
        public string USER_TYPE { get; set; }
        public string LOCATION { get; set; }
        public string EFFECTIVE_FROM { get; set; }
        public string EFFECTIVE_TO { get; set; }

    }

转换时返回异常

  

“无法将类型为'System.Decimal'的对象强制转换为类型   'System.String'“

1 个答案:

答案 0 :(得分:1)

没有从decimal到string的隐式或显式转换。你可以尝试:

string yourString = a.ToString();

其中a是十进制变量。