我不能 select 进入 EF 中的基本类型列表吗?
Can't I select into list of primitive type in EF?
我想了解为什么我的代码会抛出空引用异常。
DisruptionReportSteps
不为空,问题不在他们身上。我的猜测是,通过抛出 nre,EF 告诉它无法将我的布尔值投影到列表中,因为无法处理原始类型的集合。我说得对吗?
var disruptionReportsQuery = _dbContext.Reports
// query
.Select(report => new
{
AreStepsFilled = new List<bool>
{
report.DisruptionReportFirstStep.IsFilled,
report.DisruptionReportSecondStep.IsFilled,
report.DisruptionReportThirdStep.IsFilled,
report.DisruptionReportFourthStep.IsFilled,
report.DisruptionReportFifthStep.IsFilled,
},
})
如果我这样做:
var disruptionReportsQuery = _dbContext.Reports
// query
.Select(report => new
{
IsFirstStepFilled = report.DisruptionReportFirstStep.IsFilled,
IsSecondStepFilled = report.DisruptionReportSecondStep.IsFilled,
IsThirdStepFilled = report.DisruptionReportThirdStep.IsFilled,
IsFourthStepFilled = report.DisruptionReportFourthStep.IsFilled,
IsFifthStepFilled = report.DisruptionReportFifthStep.IsFilled,
})
有效。
不过,我想确保我理解为什么会出现这种行为。
发生这种情况是因为您试图使用查询中的值而不是具体的值列表来初始化列表。但是查询的值始终为空,它们将在查询运行时分配(例如通过调用 ToList() 或 ToArray())。
所以如果你想创建一个图元列表,你必须首先创建一个对象列表。在此之后你可以初始化列表。
var disruptionReports = _dbContext.Reports
.Select(report => new
{
IsFirstStepFilled = report.DisruptionReportFirstStep.IsFilled,
IsSecondStepFilled = report.DisruptionReportSecondStep.IsFilled,
IsThirdStepFilled = report.DisruptionReportThirdStep.IsFilled,
IsFourthStepFilled = report.DisruptionReportFourthStep.IsFilled,
IsFifthStepFilled = report.DisruptionReportFifthStep.IsFilled,
}).ToList();
var disruptionReportsList = disruptionReports
.Select(report => new
{
AreStepsFilled = new List<bool>
{
report.DisruptionReportFirstStep.IsFilled,
report.DisruptionReportSecondStep.IsFilled,
report.DisruptionReportThirdStep.IsFilled,
report.DisruptionReportFourthStep.IsFilled,
report.DisruptionReportFifthStep.IsFilled,
},
}).ToList();
我想了解为什么我的代码会抛出空引用异常。
DisruptionReportSteps
不为空,问题不在他们身上。我的猜测是,通过抛出 nre,EF 告诉它无法将我的布尔值投影到列表中,因为无法处理原始类型的集合。我说得对吗?
var disruptionReportsQuery = _dbContext.Reports
// query
.Select(report => new
{
AreStepsFilled = new List<bool>
{
report.DisruptionReportFirstStep.IsFilled,
report.DisruptionReportSecondStep.IsFilled,
report.DisruptionReportThirdStep.IsFilled,
report.DisruptionReportFourthStep.IsFilled,
report.DisruptionReportFifthStep.IsFilled,
},
})
如果我这样做:
var disruptionReportsQuery = _dbContext.Reports
// query
.Select(report => new
{
IsFirstStepFilled = report.DisruptionReportFirstStep.IsFilled,
IsSecondStepFilled = report.DisruptionReportSecondStep.IsFilled,
IsThirdStepFilled = report.DisruptionReportThirdStep.IsFilled,
IsFourthStepFilled = report.DisruptionReportFourthStep.IsFilled,
IsFifthStepFilled = report.DisruptionReportFifthStep.IsFilled,
})
有效。
不过,我想确保我理解为什么会出现这种行为。
发生这种情况是因为您试图使用查询中的值而不是具体的值列表来初始化列表。但是查询的值始终为空,它们将在查询运行时分配(例如通过调用 ToList() 或 ToArray())。
所以如果你想创建一个图元列表,你必须首先创建一个对象列表。在此之后你可以初始化列表。
var disruptionReports = _dbContext.Reports
.Select(report => new
{
IsFirstStepFilled = report.DisruptionReportFirstStep.IsFilled,
IsSecondStepFilled = report.DisruptionReportSecondStep.IsFilled,
IsThirdStepFilled = report.DisruptionReportThirdStep.IsFilled,
IsFourthStepFilled = report.DisruptionReportFourthStep.IsFilled,
IsFifthStepFilled = report.DisruptionReportFifthStep.IsFilled,
}).ToList();
var disruptionReportsList = disruptionReports
.Select(report => new
{
AreStepsFilled = new List<bool>
{
report.DisruptionReportFirstStep.IsFilled,
report.DisruptionReportSecondStep.IsFilled,
report.DisruptionReportThirdStep.IsFilled,
report.DisruptionReportFourthStep.IsFilled,
report.DisruptionReportFifthStep.IsFilled,
},
}).ToList();