This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21850|Reply: 1

[Source] Cast to value type "System.Decimal" failed...

[Copy link]
Posted on 3/11/2019 10:02:24 AM | | | |
The cast to the value type "System.Decimal" fails because the materialized value is null. The generic parameter or query of the result type must be of a type that can be null.
1. When using EF to access database summing, such exceptions are often encountered

An unhandled exception of type "System.InvalidOperationException" occurs in EntityFramework.dll



Exception Code 1:



Exception code 2:

2. Reasons:

Reason for throwing: The data filtered from the database according to the conditions is empty, and then the sum throws an exception.

Solution 1:After the filter fields or data rows are taken into memory based on the criteria, they are summed


Solution 2:This can be done if the sum results need to be specified as null when the filter data rows do not exist based on the condition



Solution 3 (recommended):Use DefaultIfEmpty()








Previous:Bayesian Thinking: Python Learning Method for Statistical Modeling, PDF Chinese version
Next:The exception is resolved by injecting the RedisTemplate instance in SpringBoot
Posted on 3/11/2019 11:38:22 AM |
I'm using option three
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com