SSD
SSD

Reputation: 1259

Unable to cast object of type 'System.Collections.Generic.List`1[System.Object]' to type 'System.Collections.Generic.List`1[customType]

I have ArrayList which contains 4 items. each item is of type List<object> . I am trying to get first item from ArrayList using below code. but it throws error

Unable to cast object of type 'System.Collections.Generic.List1[System.Object]' to type 'System.Collections.Generic.List1[CustomType]

calling code -

ArrayList arrayList = BusinessLayer.GetData();
List<CustomType> tempList = (List<CustomType>)arrayList[0];

called code logic -

if (connection.State == System.Data.ConnectionState.Closed)
                    connection.Open();

                var command = connection.CreateCommand();
                command.CommandText = "EXEC SP_GET_DATA @id";
                command.Parameters.Add(new SqlParameter("@id", id));
                using (var reader = command.ExecuteReader())
                {
                    var customTypeList = ((IObjectContextAdapter)context)
                            .ObjectContext
                            .Translate<object>(reader)
                            .ToList();

                    arrayList.Add(customTypeList);
        
                   reader.NextResult();

                   var customType2List = ((IObjectContextAdapter)context)
                            .ObjectContext
                            .Translate<object>(reader)
                            .ToList();

                    arrayList.Add(customType2List);
              }

I am returning arraylist and want to get data back at calling code. I don't want to use model in called code. I understand that, we can use model at called code but I have to verify whether using ArrayList, can we get data back? I hope I explained clearly.

here I am trying to cast List<object> from ArrayList to List<CustomType>

Upvotes: 2

Views: 14500

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 416101

The problem is this expression:

.Translate<object>(reader)

The translation result for each row is just Object instead of anything actually useful, and so when you call .ToList() on the next line, you have a list of Object instead of the type you actually want. It's possible these objects are even still some IDataReader type, where the Cast<> recommendation in other answers will not work.

You probably want this instead:

.Translate<CustomType>(reader)

Where your CustomType completes the data contract required for EF to map from the reader. I understand you might not want your business layer to know about the presentation objects, that it feels less pure, but you have to make the translation somewhere, and the earlier you do the easier and faster things can be.

I'd further recommend avoiding ArrayList here completely. At lease use List<Object> if you can't find anything more useful at a type... but it's worth quite a lot of effort to get some type system support here.

Upvotes: 0

thewallrus
thewallrus

Reputation: 725

You have to explicitly cast back to List of Object first. Then use Cast to the custom type.

ArrayList arrayList = new();

arrayList.Add(new List<object>() { new Person() { FirstName = "Bob", LastName = "NewHart"} });

List<Person> tempList = ((List<object>)arrayList[0]).Cast<Person>().ToList();

Console.WriteLine(tempList[0].FirstName);
Console.ReadLine();

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Upvotes: 0

Related Questions