我在我的数据上执行了这个查询:
newObj.TotalSizeDone =
_ctx.TestPackages.Where(
i =>
i.LineCheckState && i.TestState && i.Flushing && i.Drying && i.ReInstatement &&
i.CleaningState).Sum(i=>i.Size);
但我遇到了这个错误:
转换为值类型“System.Single”失败,因为实例化的值为空。结果类型的泛型参数或查询必须使用可空类型。
我的类:
public int Id { set; get; }
public string PackageNumber { set; get; }
public float Size { set; get; }
public string Descrption { set; get; }
public DateTime SubmitDateTime { set; get; }
public string TestPackageLocation { set; get; }
public string TestPackageOrder { set; get; }
public string LineCheckReportNumber { set; get; }
public bool LineCheckState { set; get; }
public DateTime? LineCheckSubmitDateTime { set; get; }
public string CleanReportNumber { set; get; }
public bool CleaningState { set; get; }//ndt test
public DateTime? CleanSubmitDateTime { set; get; }
public string TestReportNumber { set; get; }
public bool TestState { set; get; }
public DateTime? TestSubmitDateTime { set; get; }
public string DryingReportNumber { set; get; }
public bool Drying { set; get; }
public DateTime? DryingSubmitDateTime { set; get; }
public string FlushingReportNumber { set; get; }
public bool Flushing { set; get; }
public DateTime? FlushingSubmitDateTime { set; get; }
public string ReInstatementReportNumber { set; get; }
public DateTime? ReInstatementSubmitDateTime { set; get; }
public bool ReInstatement { set; get; }
数据:
INSERT [dbo].[TestPackages] ([Id], [PackageNumber], [Size], [Descrption], [SubmitDateTime], [TestPackageLocation], [TestPackageOrder], [LineCheckReportNumber], [LineCheckState], [LineCheckSubmitDateTime], [CleanReportNumber], [CleaningState], [CleanSubmitDateTime], [TestReportNumber], [TestState], [TestSubmitDateTime], [DryingReportNumber], [Drying], [DryingSubmitDateTime], [FlushingReportNumber], [Flushing], [FlushingSubmitDateTime], [ReInstatementReportNumber], [ReInstatementSubmitDateTime], [ReInstatement]) VALUES (1, N'TestPackage-5185', 0, N'323', CAST(0x0000A658016AB9A6 AS DateTime), N'1220', N'1', N'256', 1, CAST(0x0000A66300000000 AS DateTime), N'15', 1, NULL, N'2626', 1, CAST(0x0000A66A00000000 AS DateTime), N'150', 1, CAST(0x0000A65800000000 AS DateTime), N'21', 1, CAST(0x0000A66300000000 AS DateTime), N'212', CAST(0x0000A66300000000 AS DateTime), 0)
GO
Sum(i=>(float?)i.Size) ?? 0f
看看是否有效。 - Dennis_E