<分区>
我如何转换 System.Collection.IEnumerable
在 C# 中列出?
实际上我正在执行一个存储过程,它在 System.Collection.IEnumerable
中给我结果集我想将该结果集转换为 c# List<User>
.
注意我不想使用任何循环。有没有类型转换的方法!
<分区>
我如何转换 System.Collection.IEnumerable
在 C# 中列出?
实际上我正在执行一个存储过程,它在 System.Collection.IEnumerable
中给我结果集我想将该结果集转换为 c# List<User>
.
注意我不想使用任何循环。有没有类型转换的方法!
最佳答案
您可以使用以下内容:
IEnumerable myEnumerable = GetUser();
List<User> myList = myEnumerable.Cast<User>().ToList();
正如 Lasse V. Karlsen 在他的评论中建议的那样而不是 Cast<User>
你也可以使用 OfType<User>();
如果您的 IEnumerable 默认是通用的,我不认为这是因为您的命名空间有问题:System.Collection.IEnumerable 您可以轻松使用:
IEnumerable<User> myEnumerable = GetUser();
List<User> myList = myEnumerable.ToList();
关于c# - 如何在 C# 中将 System.Collection.IEnumerable 转换为 List<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42431646/