簡體   English   中英

在Sum中使用時,LINQ如何返回0代替NULL?

[英]How can LINQ return 0 in place of NULL when being used in Sum?

使用嵌套的Sum函數時,我的代碼遇到問題。 無法解釋NULL值的最新版本在這里:

TotalHours = Tickets.Sum(Function(x) If(x Is Nothing, 0, x.Ticket_Times.Sum(Function(y) If(y Is Nothing, 0, y.Hours))))

我也嘗試過使用CType來獲得相同的結果:

System.InvalidOperationException: 'The null value cannot be assigned to a member with type System.Decimal which is a non-nullable value type.'

TotalHours是Double,但是數據庫中的Hours是Decimal,所以也許這是引起問題的原因?

借助注釋中的一些幫助,我相信我可以使用代碼。

TotalHours = If(Tickets.Sum(Function(x) x.Ticket_Times.Sum(Function(y) If(CType(y.Hours, Decimal?) Is Nothing, 0, CType(y.Hours, Decimal?)))), 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM