CodexBloom - Programming Q&A Platform

Visual Studio 2022 - scenarios CS0131 When Using Nullable Reference Types with LINQ

πŸ‘€ Views: 0 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-14
csharp linq nullable-reference-types visual-studio-2022 C#

I'm updating my dependencies and I'm migrating some code and Hey everyone, I'm running into an issue that's driving me crazy... I'm working with a CS0131 behavior in Visual Studio 2022 when trying to use LINQ with nullable reference types. My project is targeting .NET 6, and I'm using C# 10 features. The code snippet below illustrates the scenario: ```csharp public class User { public string? Name { get; set; } } public class UserService { public List<string> GetUserNames(List<User> users) { return users.Select(u => u.Name).ToList(); // behavior CS0131 here } } ``` The behavior message states: "CS0131: The left-hand side of an assignment must be a variable, property or indexer." I can’t quite wrap my head around why this is happening since I thought LINQ should handle nullable types properly. I've already tried disabling nullable reference types in my project settings as a workaround, but that doesn't feel like a real solution. Additionally, I've verified that my LINQ query is properly formed and should be returning a list, but I keep hitting this behavior. Is there something I'm missing about how nullable types work with LINQ in this context? Any insights or solutions would be greatly appreciated! My development environment is Linux. Is there a better approach? My team is using C# for this REST API. I'd love to hear your thoughts on this. Hoping someone can shed some light on this. I'm coming from a different tech stack and learning C#. Thanks for your help in advance! For context: I'm using C# on macOS. Thanks for any help you can provide!