Avatar of dba123
dba123 asked on

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<IRole>'

We have this class defined:

namespace VU.Business.Entities
    public class KuDbRoleProvider : IRoleProvider
        public KuDbRoleProvider();

        public IEnumerable<IRole> GetDefaultRoles();
        public IRole GetRoleByName(string roleName);
        public IRole New(string roleName);
        public IEnumerable<IRole> RetrieveAllRoles();

And in another class I'm trying to implement and call the RetrieveAllRoles() and put it into some sort of collection which I tried making an array:

    class KURoleProvider : RoleProviderBase
        public override Configuration.Roles.Role[] GetRoles()
            KuDbRoleProvider roleProvider;
            BusinessInterfaces.Security.IRole[] kuRoles;
            kuRoles = roleProvider.RetrieveAllRoles();

I can't figure out how to get this working.  Here is my error:

 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<BusinessInterfaces.Security.IRole>' to 'BusinessInterfaces.Security.IRole'. An explicit conversion exists (are you missing a cast?)

Avatar of undefined
Last Comment

8/22/2022 - Mon

not that this will fix your problem explicitly but i would recommend using generic lists for this

List<KuDbRoleProvider >

IRole[] is Array. As IEnumerable<T>, they are both IEnumerable. But you cannot convert IEnumerable<T> to Array implicitly. You can declare kuRoles as IEnumerable<IRole>, that will work.

why would you use an IEnumerable in the first place to get a list of roles when you can just shove it into a frigging generic list or array?  I don't see why whoever implemented this with an Enumerable.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

It is pretty obvious, that I need to implement all the methods in the IRoleProvider which it looks like KuDbRoleProvider has not done yet.  It's just stubbed out.  I don't need to create a new class, I just need to finish the implementation on KuDbRoleProvider methods for IRoleProvider it looks like.  I just don't get why anyone would use IEnumerable to get a list of the roles from our DAL when you can just push it into an array in the first place.

nevermind I guess I was looking at some meta data of that class.  So here's the implementation of RetrieveAll

        public IEnumerable<IRole> RetrieveAllRoles()
            List<IRole> roles = new List<IRole>();

            using (IDataReaderEnumerable<DAL.Role> dalRoles = DAL.Role.RetrieveAll())
                foreach (DAL.Role dalRole in dalRoles)
                    roles.Add(new Role(dalRole));

            return roles;

Why the hell would you use IEnumerable in this case?  it's returning a generic list in the first place that can be sorted so why would you do that?  and what's this IDataReaderEnumerable...weird, I've never used this before.  I didn't implement this method and the person who did is out right now.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question