英文:
C# Cannot implicitely convert type "X" to string[][]
问题
I have a list that I'm trying to convert to a jagged array (string) and I'm getting:
我有一个列表,我试图将其转换为嵌套数组(字符串),但出现了以下错误:
> cannot implicitly convert type to string{}{}
> 无法隐式地将类型转换为 string{}{}
I have this class structure:
我有以下类结构:
internal class ProcessedZip
{
public string ErrorFound {get; set;}
public string IsEnabled {get, set;}
//6 others...
}
I have a function that processes ZipFiles (public string[][] <function name> (params)
我有一个处理Zip文件的函数(public string[][] <function name> (params))
I create a variable:
我创建了一个变量:
var processedZips = new List<ProcessedZip>();
I have a loop and within the loop I do:
我有一个循环,在循环内部执行以下操作:
processedZips.Add(new ProcessedZip
{
ErrorFound = errorFound.ToString();
IsEnabled = isEnabled.ToString();
//the other 6....
}
When the loop finishes, I want to return a jagged string array. I cannot seem to figure out how to convert my list to a jagged array. I've tried:
当循环结束时,我想返回一个嵌套的字符串数组。我似乎无法弄清楚如何将我的列表转换为嵌套数组。我尝试过:
return processedZips.ToArray();
return processZips.Select(i=> new ProcessedZip()).ToArray();
return processedZips.Select(i = new[] {i}).ToArray();
They all return:
它们都返回:
> Cannot implicitly convert BOA.Models.ProcessedZip[] to string[][]
> 无法隐式地将 BOA.Models.ProcessedZip[] 转换为 string[][]
Any help would be appreciated. Thank you.
任何帮助都将不胜感激。谢谢。
英文:
I have a list that I'm trying to convert to a jagged array (string) and I'm getting:
> cannot implicitly convert type to string{}{}
I have this class structure:
internal class ProcessedZip
{
public string ErrorFound {get; set;}
public string IsEnabled {get, set;}
//6 others...
}
I have a function that processes ZipFiles (public string[][] <function name> (params)
I create a variable:
var processedZips = new List<ProcessedZip>();
I have a loop and within the loop I do:
processedZips.Add(new ProcessedZip
{
ErrorFound = errorFound.ToString();
IsEnabled = isEnabled.ToString();
//the other 6....
}
When the loop finishes, I want to return a jagged string array. I cannot seem to figure out how to convert my list to a jagged array. I've tried:
return processedZips.ToArray();
return processZips.Select(i=> new ProcessedZip()).ToArray();
return processedZips.Select(i = new[] {i}).ToArray();
They all return:
> Cannot implicitly convert BOA.Models.ProcessedZip[] to string[][]
Any help would be appreciated. Thank you.
答案1
得分: 0
让我猜猜:您想通过添加所有6个字符串属性将单个ProcessedZip
转换为string[]
:
string[][] result = processedZips
.Select(p => new[]{ p.ErrorFound, p.IsEnabled, ... })
.ToArray();
// `string[][]`是一种其元素为数组(在这种情况下为字符串数组)的交错数组。LINQ查询选择数组,并通过`ToArray`将它们添加到`string[][]`中。
英文:
Let me guess: you want to convert a single ProcessedZip
to a string[]
by adding all 6 string properties:
string[][] result = processedZips
.Select(p => new[]{ p.ErrorFound, p.IsEnabled, ... })
.ToArray();
A jagged array like string[][]
is an array whose elements are arrays, in this case string-arrays. So the LINQ query selects arrays and with ToArray
they're added to a string[][]
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论