简体   繁体   English

C# LINQ 返回类型无效

[英]C# LINQ Invalid return type

public static IEnumerable<(string upper, string lower)> SelectByCase()
{
    string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" };

    var wordsUpperAndLower =
        from s in words
        select new { Upper = s.ToUpperInvariant(), Lower = s.ToLowerInvariant() };


    return wordsUpperAndLower;
}

Hello, I need a little help.你好,我需要一点帮助。 I'm trying to return variable wordsUpperAndLower but have issue "Cannot convert expression type 'System.Collections.Generic.IEnumerable<{string ToUpperInvariant, string ToLowerInvariant}>' to return type 'System.Collections.Generic.IEnumerable<(string upper, string lower)>'"我正在尝试返回变量wordsUpperAndLower但有问题“无法转换表达式类型 'System.Collections.Generic.IEnumerable<{string ToUpperInvariant, string ToLowerInvariant}>' 以返回类型 'System.Collections.Generic.IEnumerable<(string upper,字符串较低)>'"

What is the problem?问题是什么?

Your using an anonymous type you should be selecting using a tuple您使用的是匿名类型,您应该使用元组进行选择

public static IEnumerable<(string upper, string lower)> SelectByCase()
{
    string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" };
    //Sorry I took the liberty to use the fluent syntax
    return words.Select(x => 
    { 
       return (s.ToUpperInvariant(),s.ToLowerInvariant());
    });
}

Note: It probably can be inlined like so but I'm not near my IDE to verify whether the compiler will get confused注意:它可能可以像这样内联,但我不在我的 IDE 附近以验证编译器是否会混淆

return words.Select(s =>(s.ToUpperInvariant(), s.ToLowerInvariant()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM