user3665237
user3665237

Reputation: 11

The type 'System.Collections.Generic.IEnumerable <string>' can not be implicitly converted to 'string []'

I have a problem and I think you can help me I have this code:

string[] A1 = 
    Directory
        .GetFileSystemEntries(textbox2_f2.Text, "*", SearchOption.AllDirectories)
        .Select(System.IO.Path.GetFileName);

for (int i1 = 0; i1 < A1.Length; i1++)
    listbox_2f.Items.Add(A1[i1]);

And here is the error:

The type 'System.Collections.Generic.IEnumerable ' can not be implicitly converted to 'string []'. There is already an explicit conversion exists.

How can I solve this problem?

Upvotes: 1

Views: 1384

Answers (2)

crashmstr
crashmstr

Reputation: 28573

This code:

Directory.GetFileSystemEntries(textbox2_f2.Text, "*", SearchOption.AllDirectories)

Returns a string[], which is great.

But then you put a Select on top of that. Select returns an IEnumerable, which cannot be assigned to an array directly.

You can then call ToArray() and use as you are, but you could also use the IEnumerable more directly (and save a little time that the conversion takes).

//A1 will be IEnumerable<String>
var A1 = Directory.GetFileSystemEntries(textbox2_f2.Text, "*", SearchOption.AllDirectories)
    .Select(System.IO.Path.GetFileName);

foreach (var a in A1)
{
    listbox_2f.Items.Add(a);
}

Upvotes: 1

fordareh
fordareh

Reputation: 2963

Try:

Directory.GetFileSystemEntries(textbox2_f2.Text, "*", SearchOption.AllDirectories)
    .Select(System.IO.Path.GetFileName)
    .ToArray()

Upvotes: 3

Related Questions