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

C# 如何对列表列表进行扩展

C# 如何对列表列表进行扩展,c#,arraylist,extension-methods,C#,Arraylist,Extension Methods,如果可能的话,我想对列表列表进行扩展。我尝试过这一主题的变体,但都不起作用: public static List<List<myClass>> ToListsOfmyClass(this IEnumerable<IEnumerable<OtherClass>> listsOfOtherClass) { return listsOfOtherClass.SelectMany(many => many.Select(p => ne

如果可能的话,我想对列表列表进行扩展。我尝试过这一主题的变体,但都不起作用:

public static List<List<myClass>> ToListsOfmyClass(this IEnumerable<IEnumerable<OtherClass>> listsOfOtherClass)
{
    return listsOfOtherClass.SelectMany(many => many.Select(p => new myClass(p)).ToList()).ToList();
}
公共静态列表ToListsOfmyClass(此IEnumerable列表OtherClass)
{
返回其他类的列表。SelectMany(many=>many.Select(p=>newmyclass(p)).ToList()).ToList();
}
我收到以下编译错误:

错误CS0029无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List>”SSX.Revit.Access D:\SourceCode\Workspace\AllProjects\SSX.Revit\r2020(v.20)\Startup(plugin)\v1.0\SSX.Revit.Access\Support\AIMPoint.cs 67 Active

请注意,myClass(p)在其他地方使用时运行良好

这一事实与实际问题完全无关,错误消息应大致如下:

Cannot implicitly convert type 'System.Collections.Generic.List<T>' to 'System.Collections.Generic.List<System.Collections.Generic.List<T>>'
这需要
From
来实现方法
。从接口
IMyConvertible

请注意,myClass(p)在其他地方使用时运行良好

这一事实与实际问题完全无关,错误消息应大致如下:

Cannot implicitly convert type 'System.Collections.Generic.List<T>' to 'System.Collections.Generic.List<System.Collections.Generic.List<T>>'

这将需要
From
来实现方法
。如果可能,从接口
IMyConvertible

转换
,如何使其工作。您的返回类型可能不正确。使用SelectMany销毁内部列表时,只返回我假设的列表。如果你想要一个列表,把SelectMany改为Select。@Holger我很害怕。有没有其他办法让这样的扩展工作?如果需要,我可以重新措辞这个问题。@Fabjan喜欢。。。返回listsOfXYZs.Select(many=>many.Select(p=>newaimpoint(p)).ToList()).ToList();我们不知道什么是“有效”。您可以更改返回类型,也可以更改查询。它们不合适。如果可能的话,如何让它工作。可能你的返回类型不合适。使用SelectMany销毁内部列表时,只返回我假设的列表。如果你想要一个列表,把SelectMany改为Select。@Holger我很害怕。有没有其他办法让这样的扩展工作?如果需要,我可以重新措辞这个问题。@Fabjan喜欢。。。返回listsOfXYZs.Select(many=>many.Select(p=>newaimpoint(p)).ToList()).ToList();我们不知道什么是“有效”。您可以更改返回类型,也可以更改查询。实际上,我最终使用了一个已经拥有的扩展方法:return listsOfOtherClass.Select(x=>x.ToMyClass()).ToList();实际上,我最终使用了一个已经有的扩展方法:return listsOfOtherClass.Select(x=>x.ToMyClass()).ToList();
public static List<myClass> ToFlattenedListsOfmyClass(this IEnumerable<IEnumerable<OtherClass>> listsOfOtherClass)
{
   return listsOfOtherClass.SelectMany(x => new myClass(p)).ToList();
}
public static List<List<To>> ToListsOfmyClass<From>(this IEnumerable<IEnumerable<From>> listsOfOtherClass) where From : IMyConvertible
{
   return listsOfOtherClass.Select(x => x.Select(p => p.Convert<To>()).ToList()).ToList();
}

interface IMyConvertible { ... }