0

I have a kafka class that returns a List of Lists but when I go, I have problems starting it.

public IList<IList<SCP.Kafka.AvroSchemas.historicalDataMeasurementInfo>> equipments
{
    get
    {
        return this._equipments;
    }
    set
    {
        this._equipments = value;
    }
}

to initialize it, I use

response.historicalDataMeasurement.equipments = new  List<List<historicalDataMeasurementInfo>>();

but I get the following error

It is not possible to implicitly cast type "System.Collections.Generic.List<System.Collections.Generic.List<SCP.Kafka.AvroSchemas.historicalDataMeasurementInfo>>" to "System.Collections.Generic.IList<System.Collections.Generic.IList <SCP.Kafka.AvroSchemas.historicalDataMeasurementInfo>>". Is there an explicit cast (is there a missing cast?)

soon I get a null instance when I try to perform:

response.historicalDataMeasurement.equipments.Add(historicalDataList);
0

3 Answers 3

0

You can initialize it using:

response.historicalDataMeasurement.equipments = new  List<IList<historicalDataMeasurementInfo>>()

Note that it's a List of IList.

0

If you change IList to IEnumerable as below then also you will be able to create object.

public IEnumerable<IList<SCP.Kafka.AvroSchemas.historicalDataMeasurementInfo>> equipments
{
    get
    {
        return this._equipments;
    }
    set
    {
        this._equipments = value;
    }
}

To initialize it:

response.historicalDataMeasurement.equipments = new  List<List<historicalDataMeasurementInfo>>();
0

It appears your trouble is because you need to cast the item type within a list. To do so you can use the LINQ extensions.

using System.Linq;

// ...

yourList
    .Cast<IList<SCP.Kafka.AvroSchemas.historicalDataMeasurementInfo>>()
    .ToList();

The call to Cast<T> changes your list to an IEnumerable<T> which is why the call to ToList() is required to change it back to a list.

Not the answer you're looking for? Browse other questions tagged or ask your own question.