Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface declared as IEnumerable but not usable as such

I have an IEnumerable interface:

public interface IBrands: IEnumerable<Ibrand>

And its instanciation:

public class Brands: IBrands
{
    public Brands()
    {
        _brandsDic= new Dictionary<int, IBrand>();
    }
}

As I used Linq to retreive some data in another class, I had to return some IEnumerable:

public IEnumerable<IBrand> GetCarsBrands()
{
    return _carsDic.SelectMany(r => r.Value.Brands).ToList();
}

... but would like instead to return directly a IBrands. Would you know how? It complains about the casting of the IBrands in a IEnumerable if I try doing so.

Thanks!

like image 896
goul Avatar asked Dec 12 '25 20:12

goul


1 Answers

The IBrands is an IEnumerable<IBrand>, but not the other way around. So you can't return an IEnumerable<IBrand> as IBrands.

Depending of what Brands actually represents, you could create another instance of Brands.

return new Brands(_carsDic.Values);
like image 152
Stefan Steinegger Avatar answered Dec 14 '25 08:12

Stefan Steinegger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!