Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将泛型列表强制转换为继承的类_C#_Linq_Generic List - Fatal编程技术网

C# 将泛型列表强制转换为继承的类

C# 将泛型列表强制转换为继承的类,c#,linq,generic-list,C#,Linq,Generic List,是否可以将列表强制转换为继承列表的类 范例 public class FooCollection : List<Foo> { } List<Foo> oFooList = new List<Foo>(); FooCollection oColl = (FooCollecton)oFooList; 不,你不能在C中贬低任何东西。狗是一种动物,但不是每一种动物都是狗 如果希望能够将列表强制转换为FooCollection,则需要在后者中添加来自前者的隐式或显

是否可以将列表强制转换为继承列表的类

范例

public class FooCollection : List<Foo>
{
}

List<Foo> oFooList = new List<Foo>();
FooCollection oColl =  (FooCollecton)oFooList;

不,你不能在C中贬低任何东西。狗是一种动物,但不是每一种动物都是狗

如果希望能够将
列表
强制转换为
FooCollection
,则需要在后者中添加来自前者的隐式或显式转换。或者使用参数
IEnumerable
添加构造函数,并使用该参数调用
base(IEnumerable项)

无论如何,您不应该继承
列表

无论如何,请尝试搜索,请参见

FooCollection oColl =  (FooCollecton)oDBClass.Where(x=> x.Id == 1).ToList();