1

我正在尝试转换从 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();
}

使用该代码,我收到此错误:

错误 1 ​​无法将类型“EveAI.Live.AccountBalance[]”隐式转换为“string[]”

不知道我在这里做错了什么。

4

2 回答 2

4

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();
}
于 2013-08-17T18:10:34.253 回答
1

List<T>.ToArray方法msdn

句法:

公共 T[] ToArray()

因此,如果您有调用方法时List<AccountBalance>应该有。AccountBalance[]ToArray

尝试这个:

AccountBalance[] corpbal = cwaa.CorpAccounts(CORP_KEY, CORP_API, USER).ToArray();

正如@BenjaminGruenbaum 在评论中提到的那样,更好的选择是使用 var 关键字(msdn):

var corpbal = cwaa.CorpAccounts(CORP_KEY, CORP_API, USER).ToArray();
于 2013-08-17T18:08:44.667 回答