質問

I have this class to hold the properties of my data returned from my database call

public class Equipment
{
    public string EquipmentId { get; set; }
    public string Description { get; set; }
    public string Model { get; set; }

    public Equipment()
    {
        // Default Constructor
    }

    public Equipment(string equipmentId, string description, string model)
    {
        EquipmentId = equipmentId;
        Description = description;
        Model = model;
    }
}

Then I have another class that acts as a list of Equipment objects. My service populates this List without issue but when I try to assign values to my with a foreach loop I am getting an error: Type <> is not enumerable.

public class EquipmentNeedingService : IListSource
{
    public readonly List<Equipment> _equipmentNeedingService = new List<Equipment>();

    public void AddEquipmentToList(Equipment equipment)
    {
        _equipmentNeedingService.Add(equipment);
    }

    System.Collections.IList IListSource.GetList()
    {
        return _equipmentNeedingService;
    }

    public bool ContainsListCollection
    {
        get { return false; }
    }
}

Everything works fine with this approach through my application but in this specific scenario I am trying to use a foreach loop. I have read through a few examples but I am not finding exactly what I need and maybe there is a more efficient way to do this?

UPDATE

In the class where I am trying to use this I have a static object that holds the multiple items returned from my database call.

private static EquipmentNeedingService _equipmentNeedingService = null;

I try to use this in the OnItemDataBound event of my repeater...

foreach (var item in _equipmentNeedingService)
{
   // implementation here
}

The error I get is from the _equipmentNeedingService in the foreach loop.

役に立ちましたか?

解決

May I suggest changing your list container class to:

public class EquipmentNeedingService : List<Equipment>
{
    // no internal list is need: public readonly List<Equipment> ...();

    // no need to any AddEquipmentToList(), but if you insist:
    public void AddEquipmentToList(Equipment equipment)
    {
        this.Add(equipment);
    }
}

Later you can easily enumerate EquipmentNeedingService just like any list.

他のヒント

This is your foreach loop:

EquipmentNeedingService equipmentNeedingService = new EquipmentNeedingService();
equipmentNeedingService.Add(new Equipment(...));
foreach (Equipment eq in equipmentNeedingService.GetList())
{
}

Unless you have a strong reason for explicit implementation then avoid doing so. Instead do it implicitly:

    public IList GetList()
    {
        return _equipmentNeedingService;
    }

Now you can use a foreach: foreach (Equipment item in items.GetList()).

Otherwise, using an explicit implementation complicates things. Source

When an interface member is explicitly implemented by a class, the member can be accessed only by using a reference to the interface.

This means that in order to access an explicitly implemented interface then you would have to first cast the object to the type of interface:

foreach (Equipment item in (items as IListSource).GetList())

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top