0

我正在尝试在 Workday 的 Financial_management API 中使用“Put_Ledger”函数,但是当我尝试将其添加object[]到对象时(正如它在 API 中声明的那样),我不断收到错误消息。

Workday 对解决这个问题没有任何帮助。这是代码示例。对象被创建,然后添加到父对象:

Ledger_Only_DataType ldOnly = new Ledger_Only_DataType
{
    Actuals_Ledger_ID = "1234567",
    Can_View_Budget_Date = true
};

//Commitment_Ledger_data
Commitment_Ledger_Data__Public_Type cl = new Commitment_Ledger_Data__Public_Type
{
    Commitment_Ledger_Reference = ledgerObject,
    Enable_Commitment_Ledger = true,
    Spend_Transaction_Data = st,
    Payroll_Transaction_Data = pt
};

// This is where the error occurs:
ldOnly.Commitment_Ledger_Data = cl;     

错误信息:

“无法将类型 'CallWorkdayAPI.Financial_Management.Commitment_Ledger_Data__Public_Type' 隐式转换为 'CallWorkdayAPI.Financial_Management.Commitment_Ledger_Data__Public_Type[]”

4

3 回答 3

1

使用列表并将它们转换为数组。这更容易:

    List<Commitment_Ledger_Data__Public_Type> cls = new List<Commitment_Ledger_Data__Public_Type>();

    Commitment_Ledger_Data__Public_Type cl1 = new 
         Commitment_Ledger_Data__Public_Type
       {
           Commitment_Ledger_Reference = ledgerObject,
           Enable_Commitment_Ledger = true,
           Spend_Transaction_Data = st,
           Payroll_Transaction_Data = pt
       };

    cls.Add(cl1);

   ldOnly.Commitment_Ledger_Data = cls.ToArray();

您也可以在初始化程序中简化并执行它

于 2019-09-16T20:02:12.500 回答
0

不熟悉工作日,但我假设

ldOnly.Commitment_Ledger_Data

是一个数组Commitment_Ledger_Data__Public_Type

因此,您需要将其设置为等于该类型的数组,而当前您将其设置为等于该类型的单个对象。

Ledger_Only_DataType ldOnly = new Ledger_Only_DataType
       {
           Actuals_Ledger_ID = "1234567",
           Can_View_Budget_Date = true
       };

       //Commitment_Ledger_data
       Commitment_Ledger_Data__Public_Type cl = new 
         Commitment_Ledger_Data__Public_Type
       {
           Commitment_Ledger_Reference = ledgerObject,
           Enable_Commitment_Ledger = true,
           Spend_Transaction_Data = st,
           Payroll_Transaction_Data = pt
       };

       Commitment_Ledger_Data__Public_Type[] cls = new Commitment_Ledger_Data__Public_Type[1];

       cls[0] = cl;

       ldOnly.Commitment_Ledger_Data = cls; 
于 2019-09-16T19:57:42.283 回答
0

错误消息告诉您问题所在 - 您正在尝试将一个Commitment_Ledger_Data__Public_Type类型的单个实例分配给一个表示该类型数组 ( Commitment_Ledger_Data) 的对象。

您应该能够使用数组(将您创建的单个项目作为它的唯一成员)进行分配:

ldlOnly.Commitment_Ledger_Data = new[] {cl};

或者你可以缩短整个事情来使用初始化语法:

var ldOnly = new Ledger_Only_DataType
{
    Actuals_Ledger_ID = "1234567",
    Can_View_Budget_Date = true,
    Commitment_Ledger_Data = new[]
    {
        new Commitment_Ledger_Data__Public_Type
        {
            Commitment_Ledger_Reference = ledgerObject,
            Enable_Commitment_Ledger = true,
            Spend_Transaction_Data = st,
            Payroll_Transaction_Data = pt
        }
    }
};
于 2019-09-16T20:07:44.080 回答