views:

37

answers:

1

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 member 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

A: 

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: http://stackoverflow.com/questions/696431/linq-query-with-nullable-sum-problem/2232796#2232796

Scott Stafford