Search code examples
c#listodooint32

C# - Convert a List System.Object[] to List System.Int32[]


i'm new to C#,

I'm trying to use a function that as been created here

Actually i don't know how to return a list as array as a parameter. This function right here will use the "write" method of Odoo API.

    public void SetFieldValue(string field, object value)
    {
        var fieldAttribute = _fieldsResult.FirstOrDefault(f => f.FieldName == field);
        if (fieldAttribute == null) return;

        fieldAttribute.Changed = fieldAttribute.Changed == false;

        fieldAttribute.Value = value;
    }

So i did like this but it don't works :

  foreach (var result in results)
  {
       result.SetFieldValue("payment_method_ids", _NewPaymentLine.ToArray());
       result.Save();
  }

_NewPaymentLine is a List that i filled list this :

var _NewPaymentLine = new List<object>();
_NewPaymentLine.Add(new object[] { 4, Payment_Ids.GetField("id").Value });

All i can say its that the input value is a System.Object[] type and the output value is a System.Int32[] type.

Actually, what i return with the SetFieldValue is a System.Object[], but i think i have to return an System.Int32[], so the question is, how can i do to make my List of System.Object[] into System.Int32[]

Thanks in advance, i hope that someone can help me.


Solution

  • Not sure if this is what you pretend, but you have to create a function to convert from your Object to Int

    public List<int> DoConvert(List<Object> objectsList){
        List<int> intList = new List<int>();
        foreach(var object in objectsList)
        {
            intList.Add(object.MyNumber)       //Input your logic here
        }
        return intList;
    }