3

I am using .net 6 with Nullable Reference Type enabled, and when I use an anonymous type to get the results of a LINQ query I get a Warning Client is not null here. CS8619:Nullability of reference type in value of type <anonymous type: int ContractId, string Name, string Street> doesn't match type <anonymous type: int ContractId, string Name, string? Street>

Here is my code:

 var contracts = _dbContext.Contracts.Select(
                    c => new
                    {
                        c.ContractId,
                        c.Client.Name,
                        c.Client.Street
                    }
                ).Where(c => c.ContractId == contractId).Take(9).ToList();

What is the proper way to make the query and avoid the warning?

Luis Zenteno
  • 107
  • 1
  • 7
  • Can you please post the full code and error? – Guru Stron Dec 22 '21 at 10:17
  • I don't think this compiles, does it? You're missing a closing parentheses before .Where. Also, your Where filters on a property that wasn't selected. – Tohnmeister Dec 22 '21 at 10:27
  • Yes sorry let me edit the code, and add the full error. – Luis Zenteno Dec 22 '21 at 16:36
  • It seems that it has to do with the fact that in the model of Client, street can be null, how can I indicate in the query that for the anonymous type generated street can be null? – Luis Zenteno Dec 22 '21 at 16:51
  • Looks like a compiler issue: https://github.com/dotnet/roslyn/issues/51886. As a workaround you could try to cast `c.Client.Street` to `string?` – stargater Feb 27 '22 at 20:43

2 Answers2

0

Try this:

var contracts = _dbContext.Contracts
             .Include(x => x.Client) // Include clients
             .Where(c => c.ContractId == contractId)
             .Select(c => new
             {
                 c.ContractId,
                 Name = c.Client == null ? null : c.Client.Name, // nullable check and line up namings of properties 
                 Street = c.Client == null ? null : c.Client.Street // nullable
             })
            .Take(9)
            .ToList();

it looks Client is impossible to be null if the ClientId is [Required], the only missing is probably the subquery .Include(x => x.Client).

Dongdong
  • 2,208
  • 19
  • 28
  • Thank you Dongdong, I tried to apply your suggestion but I got an error, **An expression tree lambda may not contain a null propagating operator.** – Luis Zenteno Dec 22 '21 at 19:29
  • @LuisZenteno updated. this post can help: https://stackoverflow.com/questions/44681362/an-expression-tree-lambda-may-not-contain-a-null-propagating-operator – Dongdong Dec 22 '21 at 22:16
0

Casting to (string?) can solve the issue, i.e.

 var contracts = _dbContext.Contracts.Select(
                    c => new
                    {
                        c.ContractId,
                        c.Client.Name,
                        Street = (string?)c.Client.Street
                    }
                ).Where(c => c.ContractId == contractId).Take(9).ToList();
Andrey Stukalin
  • 5,328
  • 2
  • 31
  • 50