Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq error: "string[] does not contain a definition for 'Except'."

Tags:

c#

linq

Here's my code:

    public static string[] SplitKeepSeparators(this string source, char[] keptSeparators, char[] disposableSeparators = null)
    {
        if (disposableSeparators == null)
        {
            disposableSeparators = new char[] { };
        }

        string separatorsString = string.Join("", keptSeparators.Concat(disposableSeparators));
        string[] substrings = Regex.Split(source, @"(?<=[" + separatorsString + "])");

        return substrings.Except(disposableSeparators); // error here
    }

I get the compile time error string[] does not contain a definition for 'Except' and the best extension method overload ... has some invalid arguments.

I have included using System.Linq in the top of the source file.

What is wrong?

like image 343
Aviv Cohn Avatar asked Dec 03 '22 17:12

Aviv Cohn


1 Answers

Your substrings variable is a string[], but disposableSeparators is a char[] - and Except works on two sequences of the same type.

Either change disposableSeparators to a string[], or use something like:

return substrings.Except(disposableSeparators.Select(x => x.ToString())
                 .ToArray();

Note the call to ToArray() - Except just returns an IEnumerable<T>, whereas your method is declared to return a string[].

like image 110
Jon Skeet Avatar answered Feb 16 '23 02:02

Jon Skeet