LINQ 查询在 VS2022 和 C#10 中给出 CS8600 和 CS1662 警告

LINQ queries give CS8600 and CS1662 warnings in VS2022 and C#10

今天开始了一项新任务,将控制台应用程序迁移到 VS2022 和 C#10 并 运行 进入一个我找不到解决方法的显示停止器,除了使用编译器指令来抑制警告。我希望避免这种情况,因为这个应用程序有成百上千个使用类似语法的 LINQ 查询。以下是我 运行 的基础知识。

public static class ResponseCodes
{
    public static string MEI0066 => "ME-I0066";
    public static string MEI0069 => "ME-I0069";
    public static string MEI0070 => "ME-I0070";
}

public class DebugLog
{
    public DebugLog()
    {
        this.DebugLogId = null;
        this.LogType = string.Empty;
        this.ResponseCode = string.Empty;
        this.Explanation = string.Empty;
    }

    public DebugLog(int? p_id, string p_logType, string p_responseCode, string p_explaination)
    {
        this.DebugLogId = p_id;
        this.LogType = p_logType;
        this.ResponseCode = p_responseCode;
        this.Explanation = p_explaination;
    }

    public int? LogId { get; set; }
    public string ResponseCode { get; set; }
    public string Explanation { get; set; }
    public string LogType { get; set; }
}

public enum LogEntryTypes
{
    BlackListedIP,
    DNSQuerySuccess,
    DNSQueryFailed,
    WhiteListedIP
}

public class QueryLogs
{
    public QueryLogs()
    {
        this.DebugLogQueue = new List<DebugLog>();
    }

    public List<DebugLog> DebugLogQueue {get;set;}

    public DebugLog? Query()
    {
        DebugLog _debugLog = new DebugLog()
        {
            LogId = 0,
            LogType = LogEntryTypes.DNSQuerySuccess.ToString(),
            Explanation = "IP was valid",
            ResponseCode = ResponseCodes.MEI0070
        };

        DebugLogQueue.Add(_debugLog);

        _debugLog = new DebugLog(1, LogEntryTypes.BlackListedIP.ToString(), "This a test", ResponseCodes.MEI0066);
        DebugLogQueue.Add(_debugLog);

        _debugLog = new DebugLog(2, LogEntryTypes.DNSQueryFailed.ToString(), "This string is not null", ResponseCodes.MEI0069);
        DebugLogQueue.Add(_debugLog);

        // This LINQ query gives a CS8600 warning "Converting null literal or possible 
        // null value to non-nullable type.
        DebugLog returnValue = DebugLogQueue
            .FirstOrDefault(x =>
                (x.ResponseCode == ResponseCodes.MEI0069) 
                && (x.LogType == LogEntryTypes.DNSQueryFailed.ToString()));

        // This form uses the null-coalescing operator but gives CS1662 warning
        // "Cannot convert lambda expression to intended delegate type because some of 
        // the return types in the block are not implicitly convertible to to the
        // delegate return type."

        returnValue = DebugLogQueue
            .FirstOrDefault(x => (x.ResponseCode ?? ResponseCodes.MEI0069));

        return returnValue;
    }
}

已尝试的内容:

因为 class ResponseCodes 是静态的,它的值永远不会为空。

因为枚举 LogEntryTypes 在使用时被转换为字符串,所以它们永远不会 return 空值。

根据其他帖子的建议,我尝试了以下方法:

  1. 通过使用“static string?MEI0066 => “ME-I0066”;”
  2. 将静态 class ResponseCodes 更改为具有可为 null 的值
  3. 将 DebugLog class 的 属性 getter/setter 更改为使用“public 字符串?ResponseCode { get; set; }”
  4. 将 LogEntryTypes 更改为字符串常量,例如 dNSQueryFailed = LogEntryTypes.DNSQueryFailed,但这与使用来自 class ResponseCodes 的静态值没有什么不同。
  5. classQueryLogs 中的 Query 方法允许 return 编辑可为 null 的 DebugLog,以便它与 .FirstOrDefault 查询运算符一起使用,这不适用于 DebugLog 属性 值。
  6. 将编译器指令设置为: “#pragma warning disable CS8600 // 将 null 文字或可能的 null 值转换为不可为 null 的类型。”消除了警告和代码 运行s,但是在数百个查询中这样做肯定会在稍后回来咬我。

因此,当发出 属性 值并 returned 时,CS8600 和 CS1662 警告必须来自 LINQ 端,但对于我来说,我一直无法做到提出正确的语法来编写查询,以便警告消失。我什至不确定该从哪里进一步了解这个问题。

我是不是遗漏了什么,或者这是 C#10 中 LINQ 查询的非入门方法?

使用 net6 时的大多数问题,都是由 c# 的这个愚蠢的新特性引起的。最糟糕的是,它默认用于新项目,大多数人花费大量时间试图找出它突然停止工作或发出奇怪警告的原因。只需在您的 net 6 项目文件中做这个小改动

 <TargetFramework>net6.0</TargetFramework>
    <!--<Nullable>enable</Nullable>-->