Per Johansson
Per Johansson

Reputation: 147

Converting/handling returned IList object

I'm having issues extracting values from a returned IList and populating a combobox in windows.forms. All items in the combobox are listed as System.object.

I have done some testing;

var retList = Services.Get<IWarehouseInfo>().ExecuteSPArray("sp_executesql", dict); <-- method that returns some values.
//Tries to extract value from retlist/which is a IList<object[]> collection.

        var strList = (from o in retList select o.ToString()).ToList();
        var strList2 = retList.OfType<string>().ToList();
        var strList3 = retList.Cast<string>();
        var strList4 = retList.Where(x => x != null).Select(x => x.ToString()).ToList(); //All these seem to result in system object. 

        var bub = strList.ElementAt(2).ToString();

        var bob = strList4.ElementAt(2).ToString();
        var kupa = strList.ToArray();
        var kupo = kupa[2].ToString();

All these fail to extract anything useful.

Upvotes: 0

Views: 208

Answers (1)

Per Johansson
Per Johansson

Reputation: 147

I thank you all for any beeps given. My mistake was that I thought that the returned values were in a list of objects. But the result was an IEnumerable, so I did not check the correct vector.

I added an method extracting the values and returning it in desired format, in this case string.

 private static List<string> ToListString(IEnumerable<object[]> inparam)
    {
        var custNums = new List<string>();

        foreach (var row in inparam)
        {
            if (row[0] != null && row[0] != DBNull.Value)
                custNums.Add(row[0].ToString());
        }

        return custNums;
    }

Upvotes: 1

Related Questions