use*_*631 4 c# entity-framework
在我的示例类中,它包含IdValues int?[]
.值来自具有Id作为关键字段的其他类.
//Database class
public class SampleValues // this is a entity that i want to collect the deatil id
{
public int Id { get; set; }
public int?[] SampleDetailIdValue { get; set; }
}
public class SampleDetailValues // this is the detail entity
{
public int Id { get; set; }
}
// The error code
if (sampleDetails.Count > 0)
{
sample.IdValues = sampleDetails.Select(s => s.Id).ToArray(); // << The error occurred this line.
}
Run Code Online (Sandbox Code Playgroud)
错误是无法将类型隐式转换int[]
为int?[]
投射你的投影:
sample.IdValues = sampleDetails.Select(s => (int?)s.Id).ToArray();
Run Code Online (Sandbox Code Playgroud)
你正在投射一个int
,ToArray
给你打电话int[]
,所以简单地投射一个int?
.
或者有Cast
扩展方法:
sample.IdValues = sampleDetails
.Select(s => s.Id)
.Cast<int?>()
.ToArray();
Run Code Online (Sandbox Code Playgroud)