tags:

views:

163

answers:

3

I have a List defined like this :

public List<string> AttachmentURLS;

I am adding items to the list like this:

instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"].Value.Split(';').ToList().Where(Attachment => !String.IsNullOrEmpty(Attachment));

But I am getting this error: Cannot implicitly convert IEnumerable to List

What am I doing wrong?

+5  A: 

Move the .ToList() to the end like this

instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"].Value.Split(';').Where(Attachment => !String.IsNullOrEmpty(Attachment)).ToList();

The Where extension method returns IEnumerable<string>.

juharr
+5  A: 

The Where method returns an IEnumerable<T>. Try adding

.ToList()

to the end like so:

instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"].Value.Split(';').ToList().Where(Attachment => !String.IsNullOrEmpty(Attachment)).ToList();
smoak
I guess that means I can remove my existing .ToList from the middle of the statement?
JL
Yes, you can remove it.
smoak
A: 

The .ToList() should be at last. Because in your code you perform the .ToList() operation earlier and after that again it goes to previous state. The Where method returns an IEnumerable.

Johnny
why do you give the same answer eight hours after someone else did it?
Oliver