Thanks, I've tried your suggestion but I get a compilation error : Cannot resolve constructor 'List(System.Collections.IList)', candidates are : List(int) (in class List<Post>) List(System.Collections.Generic.IEnumerable<Metrica.Post>) (in class List<Post>)
Any idea what I can do ? On 20 mar, 14:18, Joe Enos <[email protected]> wrote: > When you're doing a direct cast like that, if the variable is not of > type List<Post> or a descendant, then the cast will not succeed. Your > "posts" variable is apparently of type Post[] or Collection<Post> or > ArrayList or something other than List<Post>, which means a direct > cast is impossible. Collection types aren't generally > interchangeable. > > What you can do is: > List<Post> myPosts = new List<Post>(posts); > > This takes the collection of Post objects inside of posts and adds > them to a new list. It's not a cast, but rather a conversion. > > On Mar 20, 3:47 am, graphicsxp <[email protected]> wrote: > > > Hello, > > > I've got two classes, CGMPost and MSMPost, which both inherits from a > > class Post. > > > I have a IList called 'posts' that contains a collection of CGMPost > > and MSMPost entities. > > > If I do that : > > > List<Post> myPosts = posts as List<Post>; > > > then myPosts is null (altough posts contains 7 entities of type > > CGMPost and MSMPost). > > > Why is that ?
