C#Linq - 无法将IEnumerable <string>隐式转换为List <string> </string> </string>

时间:2010-05-17 23:28:40

标签: c# linq

我有一个像这样定义的List:

public List<string> AttachmentURLS;

我正在将项目添加到列表中:

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

但我收到此错误:无法将IEnumerable隐式转换为List

我做错了什么?

3 个答案:

答案 0 :(得分:39)

Where方法返回IEnumerable<T>。尝试添加

.ToList()

到底如此:

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

答案 1 :(得分:7)

.ToList()移动到这样的结尾

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

Where扩展方法返回IEnumerable<string>Where将对数组有效,因此ToList之后不需要Split

答案 2 :(得分:2)

.ToList()应该是最后的。因为在您的代码中,您之前执行.ToList()操作,之后再次执行以前的状态。 Where方法返回IEnumerable。

相关问题