linq Nullable object must have a value

异常描述

复制代码
System.InvalidOperationException
  HResult=0x80131509
  Message=Nullable object must have a value.
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue()
   at System.Nullable`1.get_Value()
   at YiSha.Service.SystemManage.KpiRewordsService.<>c__DisplayClass3_1.<GetGrapeChartByUser>b__15(<>f__AnonymousType9`8 o) in D:\codes\GrapeChart\YiSha.Business\YiSha.Service\SystemManage\KpiRewordsService.cs:line 190
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at YiSha.Service.SystemManage.KpiRewordsService.GetGrapeChartByUser(Int64 user) in D:\codes\GrapeChart\YiSha.Business\YiSha.Service\SystemManage\KpiRewordsService.cs:line 190
   at YiSha.Business.SystemManage.KpiRewordsBLL.GetGrapeChartByUser(Int64 user) in D:\codes\GrapeChart\YiSha.Business\YiSha.Business\SystemManage\KpiRewordsBLL.cs:line 99
   at YiSha.Admin.Web.Controllers.HomeController.UserDashboard(String id) in D:\codes\GrapeChart\YiSha.Web\YiSha.Admin.Web\Controllers\HomeController.cs:line 84
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
复制代码

解决

在linq的select中 

方案1: 将DateTime 或者 int 等类型改成Datetime?  int?

方案2: ColumnName =  table == null ? null : (datetime?)table.column 

在lambda的FirstOrDefault中

使用 table.Where(p=> p.column  != null).FirstOrDefault(o => o.column  == value);

posted @   微风吹过~  阅读(445)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示