为什么
int? nullInt = null;
base.Response.Data = (new BusinessLogic.RefDataManager(base.AppSettingsInfo)).SelectAppData(new DC.AppData() { AppDataKey = app_data_key != string.Empty ? app_data_key : null, AppDataTypeId = app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : nullInt });
编译,但是这个
base.Response.Data = (new BusinessLogic.RefDataManager(base.AppSettingsInfo)).SelectAppData(new DC.AppData() { AppDataKey = app_data_key != string.Empty ? app_data_key : null, AppDataTypeId = app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : null});
不?第二条语句的编译错误是"无法确定条件表达式的类型,因为'int'和null之间没有隐式转换"
DC.AppData 是
public class AppData
{
[DataMember(Name = "AppDataKey")]
public string AppDataKey { get; set; }
[DataMember(Name = "AppDataTypeId")]
public int? AppDataTypeId { get; set; }
}
C# 中的三元运算符不信任您将null
表示为int?
。您必须明确告诉 C# 编译器,您的意思是null
是int?
...
base.Response.Data = (new BusinessLogic.RefDataManager(base.AppSettingsInfo)).SelectAppData(new DC.AppData() { AppDataKey = app_data_key != string.Empty ? app_data_key : null, AppDataTypeId = app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : (int?)null});
。或者说int.Parse(app_data_type_id)
通过铸造它是一个int?
......
(int?)int.Parse(app_data_type_id)
必须将任一三元屈服操作数显式转换为 int?
。
问题就在这里:
app_data_type_id != string.Empty ? int.Parse(app_data_type_id) : null
国际。解析返回一个不可为空的 int
您需要将其转换为整数吗?
(int?) int.Parse(app_data_type_id) : null