不能分配 <null>到隐式类型的局部变量 [英] Cannot assign <null> to an implicitly-typed local variable
问题描述
我想从数据表 dt_branches_global
中选择一个字段,如果 radioButton_QP
被选中,那么数据表将包含一个 string
字段,否则数据表包含 int
字段.这就是我正在做的.但是在初始化变量 Var 时出现错误.
I want to to select a field from Data table dt_branches_global
in such a way that, if radioButton_QP
is Checked then the Data table will contain a string
field otherwise the Data Table contain an int
field. This is what I am doing. But I am getting error while initializing the variable Var.
var AllBranch_IDs = null;
if (radioButton_QP.Checked == true)
AllBranch_IDs = dt_branches_global.AsEnumerable().Select(x => x.Field<string>("BusinessSectorID")).ToArray();
else
AllBranch_IDs = dt_branches_global.AsEnumerable().Select(x => x.Field<int>("BusinessSectorID")).ToArray();
推荐答案
编译器仍然是强类型的,所以它需要弄清楚类型.编译器无法推断您将其分配给 null 的类型.然后您稍后尝试将其分配给两种不同的类型.一个整数数组和一个字符串数组.
The compiler is still strongly typed so it needs to figure out the type. It is impossible for the compiler to infer the type of you assign it to null. Then you later on try to assign it to two different types. An array of ints and an array of strings.
尝试类似:
string[] AllBranch_IDs = null;
if (radioButton_QP.Checked == true)
AllBranch_IDs = dt_branches_global.AsEnumerable().Select(x => x.Field<string>("BusinessSectorID")).ToArray();
else
AllBranch_IDs = dt_branches_global.AsEnumerable().Select(x => x.Field<int>("BusinessSectorID").ToString()).ToArray();
这篇关于不能分配 <null>到隐式类型的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!