-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Open
Milestone
Description
Currently CS8602 (Dereference of possibly null reference) is triggered for nullable properties in linq expressions, even though no NRE possible when query is executed on DB side.
Example 1 (Linq):
class User
{
public string FirstName {get;set;}
public string? MiddleName {get;set;}
public string LastName {get;set;}
}
...
string searchTerm = "...";
await dbContext.Users
.Where(u => u => u.FirstName.Contains(searchTerm)
|| u.MiddleName.Contains(searchTerm) <---------- CS8602 is reported, as 'MiddleName' is nullable
|| u.LastName.Contains(searchTerm))
.ToListAsync();Example 2 (Includes):
class SomeModel
{
public class NavigationModel Navigation? {get;set;}
}
class NavigationModel
{
public class AnotherNavigationModel DeeperNavigation {get;set;}
}
dbContext.Models
.Include(m => m.Navigation)
.ThenInclude(n => n.DeeperNavigation) <----- CS8602 is reported, as 'n' is nullableSuggestion:
Create DiagnosticSuppressor for CS8602 for IQueryable expressions in Linq methods and for Include/ThenInclude methods.
Reactions are currently unavailable