Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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# - Fatal编程技术网

C# 从抽象类型列表中获取特定类型的所有项

C# 从抽象类型列表中获取特定类型的所有项,c#,C#,我有一个包含不同类型对象的抽象对象列表。 我试图抓取某一类型的所有项目,并将这些项目设置为它们自己的列表 这不起作用- //myAbstractItems is a List<myAbstractItem> //typeAList inherents from myAbstractItem var typeAList = ((List<itemTypeA>)myAbstractItems.Where(i => i.GetType() == typeof(item

我有一个包含不同类型对象的抽象对象列表。 我试图抓取某一类型的所有项目,并将这些项目设置为它们自己的
列表

这不起作用-

//myAbstractItems is a List<myAbstractItem>
//typeAList inherents from myAbstractItem


var typeAList = ((List<itemTypeA>)myAbstractItems.Where(i => i.GetType() == typeof(itemTypeA)).ToList());
//myAbstractItems是一个列表
//myAbstractItem的类型列表固有项
var typelist=((List)myAbstractItems.Where(i=>i.GetType()==typeof(itemTypeA)).ToList());

铸造
(列表)
似乎失败。

尝试使用
其中的
方法:

var typeAList = myAbstractItems.Where(i => i.GetType() == typeof(itemTypeA)).Select(item => item as itemTypeA).ToList())

尝试以这种方式使用
Where

var typeAList = myAbstractItems.Where(i => i.GetType() == typeof(itemTypeA)).Select(item => item as itemTypeA).ToList())

好的旧循环应该很好:

List<itemTypeA> res = new List<itemTypeA>();
foreach(var item in myAbstractItems)
{
  itemTypeA temp = item as itemTypeA;
  if (temp != null)
    res.Add(temp)
}
List res=new List();
foreach(myAbstractItems中的var项)
{
itemTypeA temp=作为itemTypeA的项目;
如果(温度!=null)
资源添加(临时)
}

一个好的旧循环应该很好:

List<itemTypeA> res = new List<itemTypeA>();
foreach(var item in myAbstractItems)
{
  itemTypeA temp = item as itemTypeA;
  if (temp != null)
    res.Add(temp)
}
List res=new List();
foreach(myAbstractItems中的var项)
{
itemTypeA temp=作为itemTypeA的项目;
如果(温度!=null)
资源添加(临时)
}
使用:

var-typelist=myAbstractItems.OfType().ToList();
从文档中

OfType(IEnumerable)方法只返回源中可以转换为类型TResult的元素

使用:

var-typelist=myAbstractItems.OfType().ToList();
从文档中

OfType(IEnumerable)方法只返回源中可以转换为类型TResult的元素


这将适用于所有
itemTypeA
s(以及更多派生类型)


编辑:根据罗琳的评论进行编辑。

这将适用于所有
itemTypeA
s(以及更多派生类型)


编辑:根据罗琳的评论进行编辑。

另一种方法是使用OfType()方法:


请记住,如果源集合的任何元素为空引用,则此操作将失败。

另一种方法是使用OfType()方法:



请记住,如果源集合的任何元素都是空引用,则此操作将失败。

如果您获取类型为
itemTypeA
的元素或派生类型的元素,或者获取类型为
itemTypeA
的元素,则此操作是否重要?如果获取类型为
itemTypeA
的元素或派生类型的元素,则此操作是否重要
itemTypeA
确切地说?这与(尝试的)原始代码有细微的不同,因为它将返回任何可以指定为
itemTypeA
的内容,而不仅仅是该确切类型的内容。您是对的,但问题中的代码和问题是不明确的^^^是的,抱歉,我的意思是在我的评论中补充说,您的版本很可能是OP想要的:p这与(尝试的)原始代码有细微的不同,因为它将返回任何可以指定为
itemTypeA
,而不仅仅是该类型的内容。您是对的,但问题中的代码和问题是不明确的^^^是的,道歉,我的意思是在我的评论中补充说,你的版本很可能是OP想要的:为什么不把
Select
放在
Where
之前,不把
as
强制转换两次?为什么不把
Select
放在
Where
之前,不把
as
强制转换两次?最后一个例子会给出一个例子
列表
,而不是
列表
,这就是原始海报所说的。不过,记住
null
项发生了什么很有趣。最后一个示例给出的是
列表,而不是
列表,这正是原始海报所说的。不过,记住
null
项发生的情况很有趣。除了最后一个
Select
,您还可以执行
.Cast()
。除了最后一个
Select
,您还可以执行
.Cast()
。回答得好!谢谢为了完整起见,此解决方案具有以下(良好)属性:(1)从
itemTypeA
派生的类型元素也包括在新列表中。(这里
itemTypeA
被认为是
类型。如果它是通用接口类型、通用委托类型或数组类型,也会考虑协变和/或逆变。)(2)任何
null
条目没有运行时类型,都不会包含在新列表中。回答得好!谢谢为了完整起见,此解决方案具有以下(良好)属性:(1)从
itemTypeA
派生的类型元素也包括在新列表中。(此处
itemTypeA
已知为
类型。如果它是通用接口类型、通用委托类型或数组类型,也将考虑协变和/或逆变。)(2)任何
null
项没有运行时类型,都不包括在新列表中。
var typeAList = myAbstractItems.OfType<itemTypeA>().ToList();
var typeAList = myAbstractItems.Where(i=>i is itemTypeA).Select(i=>i as itemTypeA).ToList();