Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Linq-无法隐式转换IEnumerable<;字符串>;列出<;字符串>;_C#_Linq - Fatal编程技术网

C#Linq-无法隐式转换IEnumerable<;字符串>;列出<;字符串>;

C#Linq-无法隐式转换IEnumerable<;字符串>;列出<;字符串>;,c#,linq,C#,Linq,我有一个如下定义的列表: public List<string> AttachmentURLS; 但我得到了这个错误:无法隐式地将IEnumerable转换为List 我做错了什么?像这样将.ToList()移动到末尾 instruction.AttachmentURLS = curItem .Attributes["ows_Attachments"] .Value .Split(';') .Where(Attachment => !Strin

我有一个如下定义的列表:

public List<string> AttachmentURLS;
但我得到了这个错误:无法隐式地将IEnumerable转换为List

我做错了什么?

像这样将
.ToList()
移动到末尾

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

Where扩展方法返回
IEnumerable
Where
将在数组上工作,因此
ToList
Split
后不需要
ToList

Where方法返回
IEnumerable
。尝试添加

.ToList()
为此:

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

.ToList()
应该是最后一个。因为在您的代码中,您在前面执行了
.ToList()
操作,之后它再次进入上一个状态。Where方法返回一个IEnumerable。

我想这意味着我可以从语句中间删除现有的.ToList?出现了这个错误,用谷歌搜索了它,显然我已经对它进行了升级,以前也在这里,我需要更多的咖啡因。你尝试添加类型转换了吗?instruction.AttachmentURLS=(列表)curItem.Attributes。。。哦,当然,我忘记了.ToList()方法。把这个加在最后。为什么你在别人做了八小时后给出同样的答案?
instruction.AttachmentURLS = curItem.Attributes["ows_Attachments"]
  .Value.Split(';').ToList()
  .Where(Attachment => !String.IsNullOrEmpty(Attachment))
  .ToList();