简体   繁体   English

C#,无法从“ System.Collections.Generic.IEnumerable”转换为“ string []”

[英]C#, cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'

I have some C# code: 我有一些C#代码:

var oldLines = System.IO.File.ReadAllLines(path);
var newLines = oldLines.Where(line => !line.Contains(wordToDelete));
System.IO.File.WriteAllLines(path, newLines);

The code works in a new windows application. 该代码可在新的Windows应用程序中使用。 But when I paste that code into my existing app, I get these errors: 但是,当我将该代码粘贴到现有应用程序中时,出现以下错误:

Error   2   Argument 2: cannot convert from
'System.Collections.Generic.IEnumerable<string>' to 'string[]'
Error   1   The best overloaded method match for
'System.IO.File.WriteAllLines(string, string[])' has some invalid
arguments

Why would this error be thrown in a new project, but not in my old project? 为什么会在新项目中引发此错误,而不是在我的旧项目中引发该错误?

oldLines.Where(line => !line.Contains(wordToDelete)); oldLines.Where(line =>!line.Contains(wordToDelete)); returns a IEnumerable< string> 返回IEnumerable <字符串>

System.IO.File.WriteAllLines(path, newLines.ToArray());

will fix it, 会解决的

This is probably caused by another framework version target. 这可能是由另一个框架版本目标引起的。

newLines is an IEnumerable<string> not a string[] , but your .NET version (I assume 3.5) does not have the overload which accepts an IEnumerable<String> , that was introduced in .NET 4. newLinesIEnumerable<string>而不是string[] ,但是您的.NET版本(我认为是3.5)没有接受 .NET 4中引入IEnumerable<String>重载

So you just need to create a string[] for File.WriteAllLines or use at least .NET 4: 因此,您只需要为File.WriteAllLines创建一个string[]或至少使用.NET 4:

System.IO.File.WriteAllLines(path, newLines.ToArray());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TreeView:无法从&#39;System.Collections.Generic.IEnumerable转换 <string> &#39;到&#39;System.Collections.Generic.IEnumerable - TreeView: Cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.IEnumerable 无法从&#39;System.Collections.Generic.IEnumerable转换为System.Collections.Generic.IEnumerable <string> - Cannot convert from 'System.Collections.Generic.IEnumerable to System.Collections.Generic.IEnumerable<string> 无法从“ System.Collections.Generic.IEnumerable”转换为System.Collections.Generic.IEnumerable <string> - cannot convert from 'System.Collections.Generic.IEnumerable' to System.Collections.Generic.IEnumerable<string> 无法从&#39;System.Collections.Generic.IEnumerable转换 <char> &#39;到&#39;字符串&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<char>' to 'string' 无法从“System.Collections.Generic.IEnumerable”转换<string> &#39; 到 &#39;T&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'T' C# 无法将“字符串”转换为“System.Collections.Generic.IEnumerable”<string></string> - C# cannot convert 'string' to 'System.Collections.Generic.IEnumerable<string> 无法从&#39;System.Collections.Generic.IEnumerable转换 <int> &#39;到&#39;System.Collections.Generic.IEnumerable <int?> “ - Cannot convert from 'System.Collections.Generic.IEnumerable<int>' to 'System.Collections.Generic.IEnumerable<int?>' 无法从&#39;System.Collections.Generic.IEnumerable转换 <System.Collection.Generic.List<char> &gt;字符串[] - Cannot convert from 'System.Collections.Generic.IEnumerable<System.Collection.Generic.List<char>> to string[] 从&#39;string&#39;转换为&#39;System.Collections.Generic.IEnumerable <string> - Convert from 'string' to 'System.Collections.Generic.IEnumerable<string> C#:错误-无法将类型&#39;int&#39;隐式转换为&#39;System.Collections.Generic.IEnumerable <double> “ - C#: Error-Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<double>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM