A null value cannot be assigned to a member of type System.Decimal, which is a non-null value type.

I have a job registration table and there are no entries in this table.

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 causes the following error:

A null value cannot be assigned to a member of type System.Decimal, which is a type with an invalid value

Please tell me how I can solve this problem, I prefer VB.NET code

+3
source share
1 answer

You need to make sure that it performs overloading with a zero value of Max:

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

. : http://weblogs.asp.net/zeeshanhirani/archive/2008/07/15/applying-aggregates-to-empty-collections-causes-exception-in-linq-to-sql.aspx

: Linq

+2

Source: https://habr.com/ru/post/1764819/


All Articles