无法将类型 'F_M.Commitment_Ledger_Data__Public_Type' 隐式转换为 'F_M.Commitment_Ledger_Data__Public_Type[]'

Cannot implicitly convert type 'F_M.Commitment_Ledger_Data__Public_Type' to 'F_M.Commitment_Ledger_Data__Public_Type[]'

我正在尝试在 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;     

错误信息:

"Cannot implicitly convert type 'CallWorkdayAPI.Financial_Management.Commitment_Ledger_Data__Public_Type' to 'CallWorkdayAPI.Financial_Management.Commitment_Ledger_Data__Public_Type[]"

不熟悉 Workday,但我假设

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; 

使用列表并将它们转换为数组。更简单:

    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();

您也可以在初始化程序中进行简化和执行

错误消息告诉您问题所在 - 您正在尝试将 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
        }
    }
};