How to get rid of null reference warning for EF Core outer join

50 Views Asked by At

I am writing a query to retrieve a list of notification types and separate counts for the two types of subscribers they can have. I am new to EF Core but I was finally able to get it working. However, I am getting a Visual Studio warning

Dereference of a possibly null reference

I can, of course, use #pragma warning disable to suppress the warning. But warnings are usually telling me that I'm doing something wrong, so I'd rather address the problem than hide it. However, the things that I have tried so far have only turned it from a warning into an error. I am out of ideas, so I thought I would see if the community had any.

Below is the code. s.SubscribedUserCount near the bottom is the line I'm getting the warning on.

// code to get the two counts we need
var subscriptionQuery = _dbContext.Subscriptions
        .Where(x => x.SiteEntityId == request.SiteGuid)
        .GroupBy(x => x.NotificationTypeEntityId, (key, group) => new {
            Id = key,
            SubscribedUserCount = group.Count(p => p.SubscriberType == Domain.Enums.SubscriberType.User),
            SubscribedRoleCount = group.Count(p => p.SubscriberType == Domain.Enums.SubscriberType.Role)
        });

// now outer join to that code from NotificationTypes to get results we need
var results = await _dbContext.NotificationTypes
        .GroupJoin(subscriptionQuery,
            types => types.Id,
            subs => subs.Id,
            (types, subs) => new { types, subs })
        .SelectMany(x => x.subs.DefaultIfEmpty(),
            (t, s) => new GetNotificationTypesDto {
                Id = t.types.Id,
                Description = t.types.Description,
                Name = t.types.Name,
                WorkflowId = t.types.WorkflowId,
                // #pragma warning disable CS8602 // Dereference of a possibly null reference.
                SubscribedUserCount = s.SubscribedUserCount,
                // #pragma warning restore CS8602 // Dereference of a possibly null reference.
                SubscribedRoleCount = s.SubscribedRoleCount,
            })
        .ToListAsync(cancellationToken);

The original code was actually this:

SubscribedUserCount = s == null ? 0 : s.SubscribedUserCount,
SubscribedRoleCount = s == null ? 0 : s.SubscribedRoleCount,

because I wanted SubscribedUserCount and SubscribedRoleCount to be of type int. That caused no warning or syntax error, but I got a runtime error:

System.InvalidOperationException : Nullable object must have a value

I eventually gave up and made those properties nullable int instead and went with the code I posted above:

SubscribedUserCount = s.SubscribedUserCount,
SubscribedRoleCount = s.SubscribedRoleCount,

thinking it would solve the problem. And it did, in that the code works, but it gives me that warning described above. To get rid of that, I tried:

SubscribedUserCount = s?.SubscribedUserCount,
SubscribedRoleCount = s?.SubscribedRoleCount,

Unfortunately, now I get an error:

An expression tree lambda may not contain a null propagating operator

Maybe there is no solution, and I can certainly go with the #pragma disable, but I loathe using them, so I am hoping someone has a better idea.

0

There are 0 best solutions below