Pavel.Shymko
Pavel.Shymko

Reputation: 9

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)>'"

What is the problem?

Upvotes: 0

Views: 67

Answers (1)

johnny 5
johnny 5

Reputation: 21031

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

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

Upvotes: 1

Related Questions