无法将 System.LinQ.IOrderedEnumerable 类型隐式转换为 MyClassCollection。存在隐式转换(您是否缺少演员表?)

Pan*_*kaj 2 c# c#-4.0

我的项目中有以下课程。

[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>
{
}
Run Code Online (Sandbox Code Playgroud)

问题区

MyClassCollection objList = 
MyClassName.MyFunctionName().OrderByDescending(i => i.MyPropertyName);
Run Code Online (Sandbox Code Playgroud)

这一行给出了编译错误。

Cannot implicitly convert type System.LinQ.IOrderedEnumerable<MyClass>
to MyClassCollection. An implicit conversion exists(are you missing a
cast?)
Run Code Online (Sandbox Code Playgroud)

编辑

我无权更改架构设计。

Jon*_*eet 5

不太清楚为什么您希望它起作用,但您可以使用:

MyClassCollection objList = new MyClassCollection();
objList.AddRange(MyClassName.MyFunctionName()
                            .OrderByDescending(i => i.MyPropertyName));
Run Code Online (Sandbox Code Playgroud)

我个人不喜欢从一开始就派生新集合List<T>,也不喜欢从泛型类派生非泛型类只是为了固定类型参数,但如果这是你必须接受的架构......