Exception: Nullable object must have a value while typecasting in a Linq query

-1

I have a Linq statement as below

if (!inspectionItemIds.Contains((int)item.InspectionItemId)) {
    inspectionItemIds.Add((int)item.InspectionItemId);
}

And item.InspectionItemId is coming as null and its throwing me exception as below:

System.InvalidOperationException
  HResult=0x80131509
  Message=Nullable object must have a value.
  Source=mscorlib
  StackTrace:
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Nullable`1.get_Value()
   at IMS.Model.DomainLogic.ViolationManager.<>c__DisplayClass19_0.<ViolationsFor>b__9(Violation item) in C:\SourceCode\IMS\Development\IMS\IMS.Model\DomainLogic\ViolationManager.cs:line 254
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at IMS.Model.DomainLogic.ViolationManager.ViolationsFor(String for, Int32 id) in C:\SourceCode\IMS\Development\IMS\IMS.Model\DomainLogic\ViolationManager.cs:line 253
   at IMS.Web.Controllers.CaseController.Details(Int32 id, Boolean isPrint) in C:\SourceCode\IMS\Development\IMS\IMS.Web\Controllers\CaseController.cs:line 382
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()

Any help to resolve these types of issues? Any help would be very very helpful - thanks in advance.

c#
linq
asked on Stack Overflow Dec 4, 2019 by Abdul • edited Dec 4, 2019 by Olivier Jacot-Descombes

2 Answers

0

With inspectionItemIds declared as List<int> and item.InspectionItemId as int?:

You can test item.InspectionItemId and convert it to an int with pattern matching before testing with Contains

if (item.InspectionItemId is int id && !inspectionItemIds.Contains(id)) {
    inspectionItemIds.Add(id);
}

It saves you from having to do the cast twice and tests whether it is not null at the same time.

0

Here is how I resolved it, thank you so much for everybody who came to help me - thanks a lot.

 violations.ToList().ForEach(item =>
            { if ((item.InspectionItemId != null) && !inspectionItemIds.Contains((int)item.InspectionItemId)) inspectionItemIds.Add((int)item.InspectionItemId); });
answered on Stack Overflow Dec 4, 2019 by Abdul

User contributions licensed under CC BY-SA 3.0