如何将System.Collection.IEnumerable
转换为c#中的列表? 其实我正在执行一个商店程序,它给我结果集System.Collection.IEnumerable
,我想将结果集转换为c#List<User>
。如何将System.Collection.IEnumerable转换为c#中的列表<T>?
注意我不想使用任何循环。有没有类型投射的方法!
如何将System.Collection.IEnumerable
转换为c#中的列表? 其实我正在执行一个商店程序,它给我结果集System.Collection.IEnumerable
,我想将结果集转换为c#List<User>
。如何将System.Collection.IEnumerable转换为c#中的列表<T>?
注意我不想使用任何循环。有没有类型投射的方法!
您可以使用以下方法:
IEnumerable myEnumerable = GetUser();
List<User> myList = myEnumerable.Cast<User>().ToList();
由于拉塞·五卡尔森建议在他的评论,而不是Cast<User>
你也可以使用OfType<User>();
如果您的IEnumerable的默认情况下一般我不要以为你的命名空间有问题:System.Collection.IEnumerable你可以很容易地使用:
IEnumerable<User> myEnumerable = GetUser();
List<User> myList = myEnumerable.ToList();
你应该可以使用Linq的ToList()方法。
List<string> collection = myEnumCollection.ToList();
这不会工作,因为OP要求System.Collection.IEnumerable和不使用System.Collections.Generic.IEnumerable
不能'ToList()'做? –
@CircleHsiao这不是重复的。请注意作者请求IEnumerable,而不是IEnumerable。 –
Sebi
@CircleHsiao这不是重复的,因为你的问题提到IEnumerable,但作者指的是IEnumerable。 –
Sebi