C# 无法隐式地将类型“F_M.承诺\分类账\数据\公共\类型”转换为“F_M.承诺\分类账\数据\公共\类型[]”

C# 无法隐式地将类型“F_M.承诺\分类账\数据\公共\类型”转换为“F_M.承诺\分类账\数据\公共\类型[]”,c#,workday-api,C#,Workday Api,我试图在Workday的财务管理API中使用Put_Ledger函数,但当我尝试按照API中的说明将对象[]添加到对象中时,我不断收到一个错误 Workday对解决这个问题没有帮助。下面是一个代码示例。将创建对象,然后将其添加到父对象: Ledger_Only_DataType ldOnly = new Ledger_Only_DataType { Actuals_Ledger_ID = "1234567", Can_View_Budget_Date = true }; //C

我试图在Workday的财务管理API中使用Put_Ledger函数,但当我尝试按照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\u Management.Commission\u Ledger\u Data\u Public\u type”隐式转换为“CallWorkdayAPI.Financial\u Management.Commission\u Ledger\u Data\u Public\u type[]


不熟悉Workday,但我假设

ldOnly.Commitment_Ledger_Data
是一个数组:承诺\分类账\数据\公共\类型

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

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

您也可以在初始值设定项中简化并执行此操作

错误消息告诉您问题所在-您正在尝试将承诺\分类账\数据\公共\类型的单个实例分配给表示该类型承诺\分类账\数据数组的对象

您应该能够使用数组来执行分配,并将您创建的单个项改为它的唯一成员:

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
        }
    }
};