我正在尝试转换从 API 中提取的列表并将其转换为列表。该列表确实返回其他数据,但我有代码只返回我想要的数据(它可能是错误的)
//this pulls the data
public List<AccountBalance> CorpAccounts(int CORP_KEY, string CORP_API, int USER)
{
List<AccountBalance> _CAccount = new List<AccountBalance>();
EveApi api = new EveApi(CORP_KEY, CORP_API, USER);
List<AccountBalance> caccount = api.GetCorporationAccountBalance();
foreach (var line in caccount)
{
//everyting after
string apiString = line.ToString();
string[] tokens = apiString.Split(' ');
_CAccount.Add(line);
}
return _CAccount;
}
//I am trying to convert the list to the array here
private void docorpaccounts()
{
string[] corpbal = cwaa.CorpAccounts(CORP_KEY, CORP_API, USER).ToArray();
}
使用该代码我得到这个错误:
Error 1 Cannot implicitly convert type 'EveAI.Live.AccountBalance[]' to 'string[]'
不确定我在这里做错了什么。
最佳答案
您正在尝试将 AccountBalance[]
分配给 string[]
- 如错误所述。
除非您真的需要 string[]
,否则您应该将变量声明更改为 AccountBalance[]
:
private void docorpaccounts()
{
AccountBalance[] corpbal = cwaa.CorpAccounts(CORP_KEY, CORP_API, USER).ToArray();
}
或者指定如何将 AccountBalance
转换为 string
。例如使用 ToString
方法:
private void docorpaccounts()
{
string[] corpbal = cwaa.CorpAccounts(CORP_KEY, CORP_API, USER)
.Select(x => x.ToString())
.ToArray();
}
或其属性之一
private void docorpaccounts()
{
string[] corpbal = cwaa.CorpAccounts(CORP_KEY, CORP_API, USER)
.Select(x => x.MyStringProperty)
.ToArray();
}
关于c# - 使用 C# 将列表转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18291848/