来自查询的具有可为空索引的 c# 数组
c# array with nullable index from query
我有这个查询:
var result = (from i in dc.Sis
where i.Tid == tid && i.Sid == sid
select i);
我想使用此索引访问一个数组:result.num
可为空。
string ype = YPes[result.First().num]; //if index is null then ype = null
但是我收到这个错误:
Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
那么,您希望如何索引索引等于 null
的数组?它是一个有效的int?
值。这对你有意义吗?:
string ype = YPes[null];
您必须满足这两种可能的情况:
var ype = result.num.HasValue ? YPes[result.num.Value]: default(string);
在使用该变量之前进行检查。是否为空。
if(result.First().num.HasValue)
{
string ype = YPes[result.First().num];
}
我有这个查询:
var result = (from i in dc.Sis
where i.Tid == tid && i.Sid == sid
select i);
我想使用此索引访问一个数组:result.num
可为空。
string ype = YPes[result.First().num]; //if index is null then ype = null
但是我收到这个错误:
Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
那么,您希望如何索引索引等于 null
的数组?它是一个有效的int?
值。这对你有意义吗?:
string ype = YPes[null];
您必须满足这两种可能的情况:
var ype = result.num.HasValue ? YPes[result.num.Value]: default(string);
在使用该变量之前进行检查。是否为空。
if(result.First().num.HasValue)
{
string ype = YPes[result.First().num];
}