Linq无法从’System.Collections.Generic.IEnumerable ‘转换为’string ‘

我有这个代码的问题

string.Join(",", Encoding.Unicode.GetBytes("10.10.10.11").Select(x => x.ToString("X2"))); 

我收到了错误

cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'

它如何导出到逗号分隔的txt框?

您可以通过调用ToArray()扩展方法将IEnumerable转换为字符串数组:

 string.Join(",", Encoding.Unicode.GetBytes("10.10.10.11").Select(x => x.ToString("X2")).ToArray()); 

边注:

从.NET 4.0开始, String.Join的重载接受了IEnumerable ,使得对ToArray的调用过时了。

你可以试试这个ToArray()

 string.Join(",", Encoding.Unicode.GetBytes("10.10.10.11") .Select(x => x.ToString("X2"))) .ToArray();