Cannot implicitly convert type System.LinQ.IOrderedEnumerable to MyClassCollection. An implicit conversion exists(are you missing a cast?)

c#, c#-4.0

Solution

It's not really clear why you'd expect that to work, but you could use:

MyClassCollection objList = new MyClassCollection();
objList.AddRange(MyClassName.MyFunctionName()
                            .OrderByDescending(i => i.MyPropertyName));

Personally I dislike deriving new collections from `List<T>` in the first place, and also deriving non-generic classes from generic classes just to pin the type arguments, but if that's the architecture you've got to live with...

Problem

I have following classes in my project. ``` [Serializable] public class BaseEntityCollection<T> : List<T> where T : BaseEntity, new() { protected BaseEntityCollection() { } } [Serializable] public abstract class BaseEntity { protected BaseEntity() { } } public class MyClassCollection : BaseEntityCollection<MyClass> { } ``` Problem Area ``` MyClassCollection objList = MyClassName.MyFunctionName().OrderByDescending(i => i.MyPropertyName); ``` This line is giving compilation error. ``` Cannot implicitly convert type System.LinQ.IOrderedEnumerable<MyClass> to MyClassCollection. An implicit conversion exists(are you missing a cast?) ``` EDIT I don't have the privilege to change the Architecture design.

Original source