Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx>http://forums.asp.net/t/1265711.aspx/1?Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Sat, 24 May 2008 01:24:17 -040012657112377701http://forums.asp.net/p/1265711/2377701.aspx/1?Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> <span style="font-size:10pt; font-family:'Arial','sans-serif'">&nbsp;Class1 contains the RetrieveAll method that I'm trying to call from Class2.&nbsp; I just can't get this thing happy as it's not like the types I put in for my Generic list I guess.&nbsp; I've glossed over the implementation of Class1 and it's returning an IEnumerable I guess: </span><span style="font-size:10pt; font-family:'Arial','sans-serif'"> <p class="MsoNormal" style="margin:0in 0in 0pt"><span style="font-size:10pt; font-family:'Arial','sans-serif'"></span>&nbsp;</p> <p class="MsoNormal" style="margin:0in 0in 0pt"><span style="font-size:10pt; font-family:'Arial','sans-serif'"><font color="#000066"><strong>Class1</strong></font> <strong>(hhDbRoleProvider):</strong></span></p> <pre class="prettyprint">namespace xxx.hh.Business.Entities { public class <strong>hhDbRoleProvider</strong> : IRoleProvider { public IRole New(string roleName) { return new Role(this) { RoleName = roleName, Description = string.Empty }; } public IEnumerable <strong>RetrieveAllRoles()</strong> { <p> List&lt;IRole&gt; roles = new List&lt;IRole&gt;();</p> using (IDataReaderEnumerable dalRoles = Role.RetrieveAll()) foreach (Role dalRole in dalRoles) roles.Add(new Role(dalRole)); return roles; } } }</pre></SPAN> <P style="MARGIN: 0in 0in 0pt" class=MsoNormal mce_keep="true">&nbsp;</P> <P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><STRONG><FONT color=#000066>Class2</FONT></STRONG> (<STRONG>HHRoleProvider)</STRONG></SPAN></P> <P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"></SPAN>&nbsp;</P><pre class="prettyprint"><SPAN class=kwd>namespace</SPAN> xxx.hh.Common.Roles { <SPAN class=kwd>class</SPAN> <STRONG>HHRoleProvider</STRONG> : RoleProviderBase { <SPAN class=kwd>public override</SPAN> Configuration.Roles.Role[] GetRoles() { <SPAN class=cmt>// Grab all the roles from the DB</SPAN> hhDbRoleProvider roleProvider; <FONT color=#2b91af size=2><FONT color=#2b91af size=2><P> <STRONG><FONT color=#990000>List</FONT></STRONG></FONT></FONT><STRONG><FONT color=#c0c0c0 size=2><FONT color=#c0c0c0 size=2><FONT color=#000000>&lt;xxx.BusinessInterfaces.Security</FONT>.</FONT></FONT><FONT color=#809eb5 size=2><FONT color=#809eb5 size=2>IRole</FONT></FONT><FONT color=#c0c0c0 size=2><FONT color=#c0c0c0 size=2><FONT color=#000000>&gt;</FONT> <FONT color=#006600>hhRoles</FONT> = <FONT color=#000000>roleProvider.</FONT><FONT color=#990000 size=3>RetrieveAllRoles();</FONT></P></FONT></FONT></STRONG>....</pre> <p>The error is on the last line and I cannot figure out how the hell to get it to be &quot;happy&quot;:</p> <p>Cannot implicitly convert type 'System.Collections.Generic.IEnumerable&lt;xxx.BusinessInterfaces.Security.IRole&gt;' to 'System.Collections.Generic.List&lt;xxx.BusinessInterfaces.Security.IRole&gt;'. An explicit conversion exists (are you missing a cast?)</p> </span> 2008-05-23T19:53:59-04:002377715http://forums.asp.net/p/1265711/2377715.aspx/1?Re+Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Re: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> <p>You can't cast directly from IEnumerable&lt;&gt; to List&lt;&gt; because they aren't the same thing.</p> <p>You can either use LINQ's ToList() method, or instantiate a new List&lt;&gt;:</p> <p>&nbsp;</p> <p>&nbsp;</p> <pre class="prettyprint"><b>List</b><b>&lt;xxx.BusinessInterfaces.Security.IRole&gt; hhRoles = new </b><b>List</b><b>&lt;xxx.BusinessInterfaces.Security.IRole&gt;</b> (<b>roleProvider.RetrieveAllRoles());</b></pre> <p><br> &nbsp;</p> 2008-05-23T20:04:35-04:002377801http://forums.asp.net/p/1265711/2377801.aspx/1?Re+Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Re: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> I'm confused (understand that I get Generic lists but new to IEnumerable stuff and complex casting in situations like this)...and I also updated the RetrieveAllRoles() implementation in class 1&nbsp;becasue the stupid editor in here took out half of what was there which was this:<font color="#2b91af" size="2"><font color="#2b91af" size="2"></font></font><font color="#2b91af" size="2"><font color="#2b91af" size="2"> <p>List</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2">&lt;</font></font><font color="#809eb5" size="2"><font color="#809eb5" size="2">IRole</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2">&gt; roles = </font></font><font color="#888dae" size="2"><font color="#888dae" size="2">new</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"> </font></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">List</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2">&lt;</font></font><font color="#809eb5" size="2"><font color="#809eb5" size="2">IRole</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2">&gt;();</font></font></p> <p><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"><font color="#000000">So since RetrieveAllRoles is returning a Generic List of IRole, I still do not get why I can't set it to another Generic list of IRole in my Class 2.&nbsp; However the method signature on that method in class 1 says IEnumerable so what the hell is it returning?&nbsp; Is it returning a generic list of IRole or simply an IEnumerable?&nbsp; I don't see how this works.&nbsp; I know that IEnumerable lets you iterate over&nbsp;a list.&nbsp; But I guess I don't get this casting scenario.</font></p> </font></font> 2008-05-23T21:04:58-04:002377812http://forums.asp.net/p/1265711/2377812.aspx/1?Re+Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Re: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> <p>If the method signature says IEnumerable, then that's what it is returning. List&lt;T&gt; can be implicitly cast to IEnumerable&lt;T&gt;, but you'll have to explicitly cast to go from IEnumerable&lt;T&gt; to List&lt;T&gt;:</p> <p>&nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IEnumerable&lt;String&gt; ienum = new List&lt;String&gt;();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List&lt;String&gt; list = (List&lt;String&gt;)ienum;</p> 2008-05-23T21:12:39-04:002377828http://forums.asp.net/p/1265711/2377828.aspx/1?Re+Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Re: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> <p>Thanks, I'll have to research IEnumerable a bit more to really get this.&nbsp; I guess I also don't understand how it's returning an IEnumerable&lt;&gt; besides the method signature saying it's returning an IEnumerable when it's showing me that it's returning a generic list of IRole.&nbsp; So somehow the compiler pushes in An IEnumerable&lt;IRole&gt;...I just don't get that.&nbsp; The return statement in that method is saying it's returning List&lt;IRole&gt; but method signature doesn't say List&lt;IRole&gt; it says IEnumerable.&nbsp; What are the symantics of that?</p> <p>&nbsp;<strong>But moving on, I now get this</strong>:</p> <font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"></font></font><font size="2"><font color="#000000" size="2">hhDbRoleProvider</font></font><font size="2"><font size="2"><font color="#000000"> roleProvider;</font></font></font><font size="2"><font size="2"> <p></font></font><font color="#000000"><font size="2"><font size="2">List</font></font><font size="2"><font size="2">&lt;xxx.BusinessInterfaces.Security.</font></font><font size="2"><font size="2">IRole</font></font><font size="2"><font size="2">&gt; hhRoles = </font></font><font size="2"><font size="2">new</font></font><font size="2"><font size="2"> </font></font><font size="2"><font size="2">List</font></font><font size="2"><font size="2">&lt;xxx.BusinessInterfaces.Security.</font></font><font size="2"><font size="2">IRole</font></font><font size="2"><font size="2">&gt;(roleProvider.RetrieveAllRoles());</font></font></font></p> <p><strong>I also tried</strong>:</p> <font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"></font></font><font size="2"><font color="#000000" size="2">hhDbRoleProvider</font></font><font size="2"><font size="2"><font color="#000000"> roleProvider;</font></font></font><font size="2"><font size="2"> <p></font></font><font color="#000000"><font size="2"><font size="2">List</font></font><font size="2"><font size="2">&lt;xxx.BusinessInterfaces.Security.</font></font><font size="2"><font size="2">IRole</font></font><font size="2"><font size="2">&gt; hhRoles = </font></font><font size="2"><font size="2">new</font></font><font size="2"><font size="2"> </font></font><font size="2"><font size="2">List</font></font><font size="2"><font size="2">&lt;xxx.BusinessInterfaces.Security.</font></font><font size="2"><font size="2">IRole</font></font></font><font size="2"><font size="2"><font color="#000000">&gt;();</font></p> <p><font color="#000000">hhRoles = (</font></font></font><font color="#000000"><font size="2"><font size="2">List</font></font><font size="2"><font size="2">&lt;xxx.BusinessInterfaces.Security.</font></font><font size="2"><font size="2">IRole</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"><font color="#000000">&gt;)roleProvider.RetrieveAllRoles();</font></p> </font></font></font> <p><font size="2"><font color="#000000" size="2"><strong>Either way I still get this error</strong>: Use of unassigned local variable 'roleProvider'</font></font></p> <p><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"><font color="#000000">What is it talking about, I DID use it.</font><br> </p> </font></font> 2008-05-23T21:22:32-04:002377859http://forums.asp.net/p/1265711/2377859.aspx/1?Re+Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Re: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> <p>You've declared it, but you haven't assigned a value to it or instantiated it.</p> <p>hhDbRoleProvider roleProvider = new hhDbRoleProvider();</p> <p>&nbsp;<br> Or whatever constructor or method you have of instantiating hhDbRoleProvider.</p> <p>&nbsp;<br> Also, just out of curiosity, why are you creating your own role provider instead of just using the one that comes with ASP.NET?<br> &nbsp;</p> 2008-05-23T21:45:07-04:002377878http://forums.asp.net/p/1265711/2377878.aspx/1?Re+Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Re: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> <p>because the one that comes with .NET doesn't support what we're doing.&nbsp; Our portal is very advanced and we're not going to extend the asp.net membership for this.&nbsp; We're using ASP.NET MVC and this provider will hook into our CMS system.&nbsp; I can't really explain, it's that complicated of a system that we can't use the membership provider for our needs.</p> 2008-05-23T22:14:18-04:002377881http://forums.asp.net/p/1265711/2377881.aspx/1?Re+Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Re: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> <p>There is an implicit constructor I guess since the dude did not define one in class1</p> 2008-05-23T22:15:40-04:002377887http://forums.asp.net/p/1265711/2377887.aspx/1?Re+Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Re: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> <p>In the end this seems to work:</p> <font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"></font></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">hhDbRoleProvider</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"> roleProvider = </font></font><font color="#888dae" size="2"><font color="#888dae" size="2">new</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"> </font></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">hhDbRoleProvider</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2">();</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"> <p></font></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">List</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2">&lt;xxx.BusinessInterfaces.Security.</font></font><font color="#809eb5" size="2"><font color="#809eb5" size="2">IRole</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2">&gt; hhRoles = (</font></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">List</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2">&lt;xxx.BusinessInterfaces.Security.</font></font><font color="#809eb5" size="2"><font color="#809eb5" size="2">IRole</font></font><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2">&gt;)roleProvider.RetrieveAllRoles();</font></font></p> <p><font color="#c0c0c0" size="2"><font color="#c0c0c0" size="2"><font color="#000000">Thanks for your help.&nbsp; I have a long way to go to understanding casting for this and IEnumerable stuff.</font></p> </font></font> 2008-05-23T22:19:24-04:002378003http://forums.asp.net/p/1265711/2378003.aspx/1?Re+Cannot+implicitly+convert+type+System+Collections+Generic+IEnumerable+xxx+to+System+Collections+Generic+List+xxx+Re: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<xxx>' to 'System.Collections.Generic.List<xxx> <p>It is a little tricky - I've been doing C# for a couple of years and I'm still not completely clear on exactly what IEnumerable does.</p> <p>As far as I can tell, List&lt;T&gt; is pretty much a strongly typed array with a bunch of really handy extra functionality. IEnumerable&lt;T&gt; isn't an array, but rather a non-indexed collection of objects that can be enumerated (meaning looped through). IEnumerable&lt;T&gt; is the most basic interface that must be implemented by an object for it to support foreach.</p> <p>If you use LINQ, or are at least familiar with it, it makes it a little easier to understand.</p> <p>When you create a LINQ query or use a lambda expression, the result is returned as an IEnumerable&lt;T&gt; (or IQueryable&lt;T&gt;, which is nearly the same thing I believe). However, you can think of that as just a query. It doesn't actually contain any data itself, it's just sort of a pointer to it.</p> <p>The expression or query isn't actually executed until you iterate through it, or call a method like ToList(), ToArray() or ToDictionary(), which causes it to be enumerated.</p> <p>I hope that helps... good luck! :)<br> </p> 2008-05-24T01:24:17-04:00