den den
den den

Reputation: 469

Why can not I be cast?

Unable to cast object type "System.Collections.Generic.List1[NorthwindMVC3.Models.Product]" to the type of"System.Collections.Generic.IEnumerator1[NorthwindMVC3.Models.Product]".\

public IEnumerator<Product> GetEnumerator()
{     
      IEnumerable<Product> myEnumerable = Products.ToList();
      return (IEnumerator<Product>) myEnumerable;
}

public class ProductsList : IEnumerable<Product>
    {
        public ProductsList()
        {
            Products  =new List<Product>();
        }
        public List<Product> Products { get; private set; }

Why can not I?

Upvotes: 0

Views: 108

Answers (1)

Balazs Tihanyi
Balazs Tihanyi

Reputation: 6719

Try myEnumerable.GetEnumerator()

Even better: return Products.GetEnumerator();

Upvotes: 5

Related Questions