I类需要进行操作提供类型的干将IEnumerable<X>
和IEnumerable<Y>
其中X&Y是基本类型T的子类我想遍历既把它们当作类型T的内容是否有两个连接成动作,可以方便的方式被视为IEnumerable<T>
?
例:
IEnumerable<HeaderPart> headers = templateFile.MainDocumentPart.HeaderParts;
IEnumerable<FooterPart> footers = templateFile.MainDocumentPart.FooterParts;
List<OpenXmlPart> result = new List<OpenXmlPart>();
result.Concat<OpenXmlPart>(footers);
Run Code Online (Sandbox Code Playgroud)
HeaderPart和FooterPart都是OpenXmlPart的子类,但第3行失败:
'System.Collections.Generic.IEnumerable'不包含'Concat'的定义和最佳扩展方法重载'System.Linq.Enumerable.Concat(System.Collections.Generic.IEnumerable,System.Collections.Generic.IEnumerable)'有一些无效的论点
注意,我无法更改任何一个源数据,我需要创建一个新的集合 - 实际上我想做一个foreach
它.
您可以使用Cast函数转换IEnumerable<X>
为IEnumerable<T>
然后Concat
追加第二个系列
就像是:
listB.Cast<A>().Concat(listC.Cast<A>())
Run Code Online (Sandbox Code Playgroud)