我想从数据表dt_branches_global中选择一个字段,如果radioButton_QP为Checked,那么Data表将包含一个字符串字段,否则数据表包含一个int字段.这就是我在做的事情.但是我在初始化变量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的类型.然后你尝试将它分配给两种不同的类型.一个int数组和一个字符串数组.
尝试类似的东西:
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();