无法将Dictionary ValueCollection转换为IEnumerable <T>.我错过了什么?

nuh*_*003 2 c# generics dictionary

var _pool = new Dictionary<Type, Dictionary<EntityIdType, Object>>();

public IEnumerable<EntityType> GetItems<EntityType>()
{
    Type myType = typeof(EntityType);

    if (!_pool.ContainsKey(myType))
        return new EntityType[0];

    //does not work, always returns null
    // return _pool[myType].Values; as IEnumerable<EntityType>;

    //hack: cannot cast Values to IEnumarable directly
    List<EntityType> foundItems = new List<EntityType>();
    foreach (EntityType entity in _pool[myType].Values)
    {
        foundItems.Add(entity);
    }
    return foundItems as IEnumerable<EntityType>;

}
Run Code Online (Sandbox Code Playgroud)

mqp*_*mqp 7

试试这个:

return _pool[myType].Values.Cast<EntityType>();
Run Code Online (Sandbox Code Playgroud)

这具有在枚举中转换每个元素的效果.