c# - 使用 C# 将列表转换为数组

标签 c#

我正在尝试转换从 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/

相关文章:

c# - 从 WM_COPYDATA 消息编码结构

c# - 可移植类库上的 WebProxy

C# 多源、不同线程、一个事件处理程序

c# - 为什么不强制对泛型类型进行类约束?

c# - 我可以序列化 C# Type 对象吗?

c# - 具有采用不同泛型类的方法的泛型类

c# - Ninject:是否可以在 SingletonScope 中有父对象而在 TransientScope 中有子对象?

c# - 等待来自多个线程的任务

C# - EF 6 抽象导航属性

c# - AppContext.BaseDirectory vs Assembly.GetEntryAssembly().发布应用程序后位置是否相同?