我不明白为什么我不能分配从 IEnumerable<Person>
方法返回的值为同一类型。我收到消息:
Cannot convert source type
IEnumerable<Person>
to target typeIEnumerable<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/