c# - 无法将 IEnumerable 类型转换为 IEnumerable

标签 c#

我不明白为什么我不能分配从 IEnumerable<Person> 方法返回的值为同一类型。我收到消息:

Cannot convert source type IEnumerable<Person> to target type IEnumerable<Person>.

任何人都可以建议这段代码有什么问题吗:

public IEnumerable<Person> GetPeople(IEnumerable<int> Ids)
{
    return FindAll(m => Ids.Contains(m.Id)).ToList();
}

调用此方法后尝试分配给相同类型的值:

IEnumerable<Person> people = _myRepository.GetPeople(listOfIds);

我可以分配给 var,但我想在类范围内声明这个变量,所以我陷入了困境。

最佳答案

在这种情况下,您必须将 Person 与 namespace 一起使用:

public IEnumerable<[Namespace]> GetPeople(IEnumerable<int> Ids)
{
    return FindAll(m => Ids.Contains(m.Id)).ToList();
}

IEnumerable<[Namespace].Person> people = _myRepository.GetPeople(listOfIds);

[Namespace] - 存储 Person 类型的命名空间

关于c# - 无法将 IEnumerable 类型转换为 IEnumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683435/

相关文章:

c# - 返回按属性匹配的对象列表

C#:如何获取使用 Informix 插入的最后一行的 ID 号

c# - c#下创建事务调用存储过程

c# - 如何设置默认下拉列表选定项

c# - Dictionary<TKey, TValue> 什么时候调用 TKey.Equals()?

c# - 如何在不等待完成的情况下启动异步方法?

c# - 无法将对象复制到新数组中

c# - 'is' 与 try cast with null check

c# - 多次使用 C# 比较 2 个巨大的列表(有一个转折点)

c# - 使用 LINQ 获取平均值