C# Linq - Cannot implicitly convert IEnumerable to List

后端 未结 3 1726
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 18:30

I have a List defined like this :

public List AttachmentURLS;

I am adding items to the list like this:

instructio         


        
相关标签:
3条回答
  • 2021-02-18 19:02

    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.

    0 讨论(0)
  • 2021-02-18 19:04

    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();
    
    0 讨论(0)
  • 2021-02-18 19:07

    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 讨论(0)
提交回复
热议问题