我正在使用扩展方法 OrderBy 和 ThenBy 在多个字段上对自定义集合进行排序。这种排序不会影响集合,而是返回 IEnumberable。我无法将 IEnumerable 结果转换为我的自定义集合。是否有办法更改我的集合的顺序或将 IEnumerable 结果转换为我的自定义集合?
如果你的集合类型实现IList<T>
(有能力去Add()
到它)你可以编写一个扩展方法:
public static Extensions
{
public static TColl ToTypedCollection<TColl, T>(this IEnumerable ien)
where TColl : IList<T>, new()
{
TColl collection = new TColl();
foreach (var item in ien)
{
collection.Add((T) item);
}
return collection;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)