Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ not working for strings in Xamarin?

I have the following function:

    private static string SanitizeVersionStringFromUnit(string version)
    {
        var santizedString = new string(version.Where(char.IsLetterOrDigit).ToArray()); ;
        return santizedString;
    }

However, intellisense is telling me string does not contain a definition for Where and that no extension method can be found. I have using System.Linq; declared in the file. In a non-Xamarin project this code works fine.

This is a Xamarin.Forms PCL project in VS2015 Community. What gives?

like image 240
bodangly Avatar asked Apr 23 '16 17:04

bodangly


1 Answers

You are getting the error because PCL version of string does not implement IEnumerable<char>. You can use Cast to make the code compile:

private static string SanitizeVersionStringFromUnit(string version)
{
    var santizedString = new string(version.Cast<char>().Where(char.IsLetterOrDigit).ToArray());
    return santizedString;
}

See this question for more details: Why doesn't String class implement IEnumerable in portable library?

like image 79
Giorgi Avatar answered Nov 14 '22 22:11

Giorgi