如何扩展列表列表
How can I make an extension for a list of lists
如果可能的话,我想扩展列表列表。我试过这个主题的变体,但 none 奏效了:
public static List<List<myClass>> ToListsOfmyClass(this IEnumerable<IEnumerable<OtherClass>> listsOfOtherClass)
{
return listsOfOtherClass.SelectMany(many => many.Select(p => new myClass(p)).ToList()).ToList();
}
我收到以下编译错误:
Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List>' SSX.Revit.Access D:\SourceCode\WorkSpaces\AllProjects\SSX.Revit\r2020 (v.20)\Startup (plugin)\v1.0\SSX.Revit.Access\Support\AIMPoint.cs 67 Active
Note that the myClass(p) works fine as I use it elsewhere.
这个事实与实际问题完全无关,错误信息应该是这样的:
Cannot implicitly convert type 'System.Collections.Generic.List<T>' to 'System.Collections.Generic.List<System.Collections.Generic.List<T>>'
正如我们在评论中所说,您可以将方法修改为 return List<List<T>>
:
public static List<List<myClass>> ToListsOfmyClass(this IEnumerable<IEnumerable<OtherClass>> listsOfOtherClass)
{
return listsOfOtherClass.Select(x => x.Select(p => new myClass(p)).ToList()).ToList();
}
或者添加另一种方法,我们将集合展平并 return List<T>
:
public static List<myClass> ToFlattenedListsOfmyClass(this IEnumerable<IEnumerable<OtherClass>> listsOfOtherClass)
{
return listsOfOtherClass.SelectMany(x => new myClass(p)).ToList();
}
顺便说一句,在许多情况下,例如我们有 1000 个 类 这样的情况,我会考虑添加通用方法或使用像 AutoMapper
这样的工具。例如:
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 { ... }
这需要 From
从接口 IMyConvertible
实现方法 .Convert
如果可能的话,我想扩展列表列表。我试过这个主题的变体,但 none 奏效了:
public static List<List<myClass>> ToListsOfmyClass(this IEnumerable<IEnumerable<OtherClass>> listsOfOtherClass)
{
return listsOfOtherClass.SelectMany(many => many.Select(p => new myClass(p)).ToList()).ToList();
}
我收到以下编译错误:
Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List>' SSX.Revit.Access D:\SourceCode\WorkSpaces\AllProjects\SSX.Revit\r2020 (v.20)\Startup (plugin)\v1.0\SSX.Revit.Access\Support\AIMPoint.cs 67 Active
Note that the myClass(p) works fine as I use it elsewhere.
这个事实与实际问题完全无关,错误信息应该是这样的:
Cannot implicitly convert type 'System.Collections.Generic.List<T>' to 'System.Collections.Generic.List<System.Collections.Generic.List<T>>'
正如我们在评论中所说,您可以将方法修改为 return List<List<T>>
:
public static List<List<myClass>> ToListsOfmyClass(this IEnumerable<IEnumerable<OtherClass>> listsOfOtherClass)
{
return listsOfOtherClass.Select(x => x.Select(p => new myClass(p)).ToList()).ToList();
}
或者添加另一种方法,我们将集合展平并 return List<T>
:
public static List<myClass> ToFlattenedListsOfmyClass(this IEnumerable<IEnumerable<OtherClass>> listsOfOtherClass)
{
return listsOfOtherClass.SelectMany(x => new myClass(p)).ToList();
}
顺便说一句,在许多情况下,例如我们有 1000 个 类 这样的情况,我会考虑添加通用方法或使用像 AutoMapper
这样的工具。例如:
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 { ... }
这需要 From
从接口 IMyConvertible
.Convert