开发者

C# Linq - Cannot implicitly convert IEnumerable<string> to List<string>

开发者 https://www.devze.com 2022-12-31 15:25 出处:网络
I have a List defined like this : public List<string> AttachmentURLS; I am adding items to the list like this:

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 => !Stri开发者_运维知识库ng.IsNullOrEmpty(Attachment));

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

What am I doing wrong?


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();


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> and Where will work on arrays, so the ToList isn't needed after the Split.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消