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

时间:2019-09-16 19:43:58

标签: c# workday-api

我正在尝试在Workday中使用Financial_management API中的“ Put_Ledger”功能,但是当我尝试向对象中添加object[]时却不断出错(因为它在API中指出要执行)。

工作日对于解决此问题没有帮助。这是代码示例。创建对象,然后将其添加到父对象:

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 []”

3 个答案:

答案 0 :(得分: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();

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

答案 1 :(得分:0)

不熟悉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; 

答案 2 :(得分: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
        }
    }
};