简体   繁体   English

无法将源类型system.nullable转换为目标类型int

[英]Cannot convert source type system.nullable to target type int

I kept getting the error message below while trying to retrieve data using entity framework and assigning to my custom class Customer 在尝试使用实体框架检索数据并将其分配给自定义类Customer时,我一直收到以下错误消息

cannot convert source type 'system.nullable' to target type 'int' 无法将源类型“ system.nullable”转换为目标类型“ int”

CustomerNumber and Route have datatypes of Int32 and the fields in the database allows nulls CustomerNumber和Route的数据类型为Int32,并且数据库中的字段允许为空

        select new Customer()
            {
                AccountListId = j.cost1,
                Csr = j.cost2,
                CustomerName = j.cost3,
                Address = j.cost4,
                Telephone = j.cost5,
                Contact = j.cost6,
                CustomerNumber = j.cost7,
                Branch = j.cost8,
                Route = j.cost9,
            }).ToList<Customer>();

How can I handle this? 我该如何处理?

Apparently, j.cost7 and j.cost9 are of type Nullable<Int32> . 显然, j.cost7j.cost9的类型为Nullable<Int32> I am assuming, because you haven't shown us. 我想是因为您没有给我们看。

Obviously, you can't assign a Nullable<Int32> to an Int32 type, because, what do you do if the value is null ? 显然,您不能将Nullable<Int32>分配给Int32类型,因为,如果该值为null怎么办? The compiler doesn't know. 编译器不知道。 You need to decide what to do in that case, and code accordingly. 您需要确定在那种情况下该做什么,并相应地进行编码。

Let's say you decide to assign -1 as a default value in case of a null value in the database, then you can use the null-coalescing operator and do something like this: 假设您决定在数据库中值为null情况下将-1分配为默认值,然后可以使用空值合并运算符并执行以下操作:

    select new Customer()
        {
            AccountListId = j.cost1,
            Csr = j.cost2,
            CustomerName = j.cost3,
            Address = j.cost4,
            Telephone = j.cost5,
            Contact = j.cost6,
            CustomerNumber = j.cost7 ?? -1,
            Branch = j.cost8,
            Route = j.cost9 ?? -1,
        }).ToList<Customer>();

If, what you really want, is to be able to store the null value if there is one, then change the type of CustomerNumber and Route from Int32 to Nullable<Int32> , or using an alternate syntax: int? 如果您真正想要的是能够存储null值(如果存在),则将CustomerNumber的类型和RouteInt32更改为Nullable<Int32> ,或使用其他语法: int? .

Try 尝试

if (j.cost7 == null)
{
    CustomerNumber = 0;
}
else
{
    CustomerNumber = j.cost7
}

You can use the default value of an int. 您可以使用int的默认值。

Eg: 例如:

CustomerNumber = j.cost7 ?? default(int),
        Branch = j.cost8,
        Route = j.cost9 ?? default(int),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将源类型转换为System.Nullable <double> - Cannot convert source type to System.Nullable<double> 如何将源类型'System.Nullable <bool>'转换为目标类型'bool' - how to convert source type 'System.Nullable<bool>' to target type 'bool' 类型&#39;System.Int16&#39;的对象不能转换为类型&#39;System.Nullable`1 [System.Int32] - Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32] 类型&#39;System.Nullable`1 [System.Int32]&#39;的表达式不能用于类型&#39;System.Int32&#39;\\ r \\ n的构造函数参数名称:arguments [0] - Expression of type 'System.Nullable`1[System.Int32]' cannot be used for constructor parameter of type 'System.Int32'\r\nParameter name: arguments[0] 类型为“ System.Windows.Data.Binding”的对象不能转换为类型为“ System.Nullable`1 [System.Guid]” - Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Nullable`1[System.Guid]' 无法将字符串转换为目标类型系统可为null的日期时间 - cannot convert string to target type system nullable datetime 将值“null”转换为类型“System.Nullable`1[System.Int64]”时出错 - Error converting value "null" to type 'System.Nullable`1[System.Int64]' 无法将运算符“==”应用于类型“System,.Nulabe”<decimal> &#39; 和 &#39;System.Nullable<double> &#39; 错误 - Cannot apply operator '==' to type 'System,.Nullabe<decimal>' and 'System.Nullable<double>' Error 序列化复杂类型System.Nullable <System.DateTime> - Serialize Complex Type System.Nullable<System.DateTime> 无法从用户控件属性中的字符串表示形式&#39;null&#39;创建&#39;System.Nullable&#39;类型的对象 - Cannot create an object of type 'System.Nullable` from its string representation 'null' in user control proerty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM