我有以下代码:
public IList<MyObject> GetSomeData(string inputParam)
{
var temp = repository.GetData(inputParam);
var list = temp as List<MyObject>;
return list;
}
repository.GetData 的返回值为IEnumerable<IMyObject>
当我查看 temp 的值时,它有 400 多条记录。当我把它放到列表中时,它就变成了 null。为什么这个转换是不可能的?
最佳答案
它返回 null 因为 IEnumerable
实际上不是一个列表。只有当 IEnumerable
恰好是 List
而不是其他类型的序列时,转换才会成功。如果您想要一个列表,您将需要创建一个新列表并将序列中的项目添加到该列表中。
关于c# - 将 IEnumerable<object> 转换为 List<object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36165309/