c#linq中的转换为值类型'System.Single'失败

时间:2016-08-05 08:19:25

标签: c# linq

我对我的数据执行此查询:

 newObj.TotalSizeDone =
         _ctx.TestPackages.Where(
             i =>
                 i.LineCheckState && i.TestState && i.Flushing && i.Drying && i.ReInstatement &&
                 i.CleaningState).Sum(i=>i.Size);

但是我收到了这个错误:

  

转换为值类型'System.Single'失败,因为具体化值为null。结果类型的通用参数   或者查询必须使用可空类型。

我的课程:

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

1 个答案:

答案 0 :(得分:8)

问题是由于您没有任何与where子句逻辑匹配的数据(在这种情况下ReInstatementfalse),这意味着返回零结果。

如果在没有结果的情况下执行Sum,则会希望使用null结果,但Sum的返回类型将与您的float类型匹配(所以它失败了。)

为了能够Sum当没有结果时,您可以使用DefaultIfEmpty函数,如下所示,这意味着{null值永远不会有Sum 1}}担心:

var results = _ctx.TestPackages.Where( i => i.LineCheckState && 
                                            i.TestState && 
                                            i.Flushing && 
                                            i.Drying && 
                                            i.ReInstatement && 
                                            i.CleaningState);

// Sum the results, and in the event of a null value, assign 0f instead.
newObj.TotalSizeDone = results.Select(i => i.Size)
                            .DefaultIfEmpty(0f)
                            .Sum();

或者,您可以使用以下内容,以便Sum知道返回可以为空的类型(然后可以与??合并):

newObj.TotalSizeDone = results.Sum(i => (float?)i.Size) ?? 0f;