Zach
Zach

Reputation: 143

Convert 'System.Collections.Generic.IEnumerable<string>' to 'string[]

I'm having issues with converting a piece of code to work with .NET3.5.

static Dictionary<string, object> dictionary = new Dictionary<string, object> 
{
    {"key","value"},
    {"key2","value2"},
};

public static string Process(String _string)
{
    var levels = new Dictionary<string, object>
    {
        { "", "" },
        { "-", "Light" },
        { "+", "Heavy" }
    };
    var search = string.Join("and ", from l in levels.Keys
                               from w in dictionary.Keys
                               join m in _string.Split() on string.Concat(l, w) equals m
                               select string.Concat(levels[l], dictionary[w]));

    return search;
}

I'm getting two errors when I try to compile...

Argument 2: cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'

and

The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments

I thought about adding .ToArray(), but I didn't know where.

Upvotes: 2

Views: 4766

Answers (2)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

Use ToArray like this:

var search = string.Join("and ", (from l in levels.Keys
                           from w in dictionary.Keys
                           join m in _string.Split() on string.Concat(l, w) equals m
                           select string.Concat(levels[l], dictionary[w])).ToArray());

Upvotes: 4

Jason
Jason

Reputation: 21

Surround your LINQ query (select from... ) with parentheses and call .ToArray() on it.

Upvotes: 2

Related Questions