c# - 如何在 C# 中将 System.Collection.IEnumerable 转换为 List<T>?

标签 c# sql ado.net

<分区>

我如何转换 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/

相关文章:

sql - 将日期格式转换为 SELECT 中的双周存储桶

sql - 复杂SQL语句可行性

.net - 使用 ExecuteNonQuery 覆盖 SQL Server 中受影响的行?

sql-server - SQL Server 性能 - 架构与多个数据库

c# Directory.GetDirectories 不包括文件夹

c# - 在字符串列表中查找子字符串

c# - 速记访问器和修改器

mysql - sql join 中的 order by

c# - DatagridView 更改未反射(reflect)在数据库中

C# 无法将我的正则表达式识别为字符串,即使我已尝试转义所有内容