开发者

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

开发者 https://www.devze.com 2023-01-16 08:53 出处:网络
I have a Job Register Table and that table doesn\'t have any records. This is my LINQ code: Dim QRecordCount = (From LC In CntxtJobDetails.JobRegistrations _

I have a Job Register Table and that table doesn't have any records.

This is my LINQ code:

Dim QRecordCount = (From LC In CntxtJobDetails.JobRegistrations _
                            Where LC.JobCode <> 0 _
                            Select LC.JobCode).Max() + 1

When I execute the code above it gives me the following error:

The null value cannot be assigned to a mem开发者_StackOverflow社区ber with type System.Decimal which is a non-nullable value type

Please tell me how can I solve this issue, I prefer VB.NET Code


You need to make sure it runs the nullable overload of Max:

Dim QRecordCount = (From LC In CntxtJobDetails.JobRegistrations _
                            Where LC.JobCode <> 0 _
                            Select CType(LC.JobCode, Decimal?)).Max() + 1

For more information, see this link: http://weblogs.asp.net/zeeshanhirani/archive/2008/07/15/applying-aggregates-to-empty-collections-causes-exception-in-linq-to-sql.aspx

And this one: Linq query with nullable sum

0

精彩评论

暂无评论...
验证码 换一张
取 消