FirstOrDefault generating S2259 FP

Hi,

I am trying to make sure to figure out the best solution for the below piece of code. As you can see
The ‘serviceOrder’ cannot be null because it is assigned by using SingleOrDefault. I think it is a false positive. If you think otherwise please share with me how can I fix it? Our project has lots of similar major bugs generated by Sonar. Thanks!


            public async Task<Command> Handle(Query message, CancellationToken cancellationToken)
            {
Uncovered code
                var job = await this.jobRepository.GetAsync(message.JobId);
                var serviceOrder = job.ServiceOrders.FirstOrDefault(so => so.WorkOrders.Any(wo => wo.Id == message.WorkOrderId));
                var serviceProviderInfo = await this.serviceProviderService.GetServiceProviderInfo(serviceOrder.ServiceProvider_Id.Value);
'serviceOrder' is null on at least one execution path.
                return new Command
                {
                    JobId = message.JobId,
                    WorkOrderId = message.WorkOrderId,
                    Location = serviceProviderInfo.Location_Code,
                    ServiceType = serviceOrder?.RequestedServiceType_Code,
                    OfferDeliveryMethod = "SIMULTANEOUSLY",
                    DeadlineToReply = 24,
                    IsNewEntry = true
                };
            }
        }

Another example:

Hello @pinartanriverdi

FirstOrDefault can return null (that would be the default value). So it’s not a FP in my opinion.