2

this may be a simple answer.

I'm making this LINQ expression where I group a list by "Name" and then I create a collection of SymbolFields where the group key (name) is used as name and the values are joined together as the second parameter.

But the question is more, how do I avoid possible null references?

If you look at the picture, you can see that there "might" be scenarios where group.key is null.

I only want to select the ones that are NOT NULL. How can I do so based on my code?

return result
    .GroupBy(r => r.Name)
    .Select(group =>
        new SymbolField(group.Key, string.Join(string.Empty, group.Select(g => g.Symbol))));

enter image description here

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Not sure if it'd remove the warning, but you do `result.Where(r => r?.Name is not null).GroupBy(...)`. That should filter out all records that are null or have a `Name` of null. – Lolop Mar 30 '21 at 06:01
  • Hi @Lukas, it is good to attach your code as a snippet to the question, but not post it as an image. You may refer to [How to ask](https://stackoverflow.com/help/how-to-ask). Thank you. – Yong Shun Mar 30 '21 at 06:03
  • @Lolop It still complains about a possible nullreference. – Lukas Méndez Duus Mar 30 '21 at 06:06
  • @YongShun sorry, it was more to show the green line under the variable – Lukas Méndez Duus Mar 30 '21 at 06:06
  • 1
    @LukasMéndezDuus What is the **actual** error/warning you're seeing? Is this a C# CS8xxx `#nullable` warning? – Dai Mar 30 '21 at 06:06
  • 2
    @Lukas You should always include your code as text. You can include a screenshot if needs be to illustrate your point. – ProgrammingLlama Mar 30 '21 at 06:07
  • @Dai Yes that is what it is – Lukas Méndez Duus Mar 30 '21 at 06:08
  • 2
    @LukasMéndezDuus I was mostly answering your '_But the question is more, how do I avoid possible null references?_'. It looks like you are using [nullable reference types](https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references). In this case the compiler doesn't know Key cannot be null, so you can do `group!.Key` to remove the warning. Only do this if you've added the `Where` statement I mentioned, or else you could potentially get a null reference exception. – Lolop Mar 30 '21 at 06:09
  • 1
    @Lolop That's to check if `group` is nullable, not `group.Key`. As it is, `GroupBy` never yields `IGrouping?` (only `IGrouping` if the `GroupBy` `keySelector:` was `T?`), so you've got the `!` in the wrong place in your comment.# – Dai Mar 30 '21 at 06:30
  • @Dai Ah yeah, my mistake. Just saw the `name!.Length` in the link I made and assumed it was saying length cannot be null, which doesn't make any sense in retrospect. I can't edit the comment anymore unfortunately though. – Lolop Mar 30 '21 at 06:34

1 Answers1

5

I assume this is for Linq-to-Objects (not Linq-to-Entities) and the message you're seeing is a C# 8.0 #nullable warning because r.Name is typed as String? instead of String.

If so, then add a .Where( r => r.Name != null ) step, and then add a ! to the .GroupBy step because this is unfortunately one of the cases that the C# 8.0 compiler's nullability analysis isn't smart enough to detect (yet).

return result
    .Where( r => r.Name != null )
    .GroupBy( r => r.Name! )
    .Select( grp =>
        new SymbolField(
            name     : grp.Key,
            something: string.Join( separator: string.Empty, grp.Select( g => g.Symbol ) )
        )
    );
Dai
  • 141,631
  • 28
  • 261
  • 374
  • 2
    For explanation on `r.Name!`, post fix **`!`** read [What does **`null!`** statement mean?](https://stackoverflow.com/questions/54724304/what-does-null-statement-mean). – Drag and Drop Mar 30 '21 at 06:18