C# 我想将IEnumerable转换为List或IEnumerable

C# 我想将IEnumerable转换为List或IEnumerable,c#,.net,linq,list,ienumerable,C#,.net,Linq,List,Ienumerable,我有一个来自服务的IEnumerable返回列表,需要映射到对象类型的IEnumerable。这是一个直接映射: 但我得到了以下错误: System.Linq.Enumerable+WhereSelectEnumerableIterator`2 我尝试先使用Singe/First,但应用程序无法自行运行,因此对我没有帮助 private IEnumerable<Doctor> From(IEnumerable<DoctorsList> doctorList) {

我有一个来自服务的
IEnumerable
返回列表,需要映射到对象类型的IEnumerable。这是一个直接映射:

但我得到了以下错误:

System.Linq.Enumerable+WhereSelectEnumerableIterator`2
我尝试先使用Singe/First,但应用程序无法自行运行,因此对我没有帮助

private IEnumerable<Doctor> From(IEnumerable<DoctorsList> doctorList)
{
    var doctors = new List<Doctor>
    {
        new Doctor()
        {
            Name = doctorList.Select(e => e.Name).ToString() ?? null,
            Id = doctorList.Select(e => e.Id).ToString() ?? null
        }
    };

    return doctors;
}
private IEnumerable From(IEnumerable doctorList)
{
var医生=新名单
{
新医生()
{
Name=doctorList.Select(e=>e.Name).ToString()为空,
Id=doctorList.Select(e=>e.Id).ToString()为空
}
};
返回医生;
}

我认为您可以通过使用一个简单的
选择来实现这一点,查看下面的代码:

private IEnumerable<Doctor> From(IEnumerable<DoctorsList> doctorList)
{
   return  doctorList.Select(x=> new Doctor()
                                {
                                   Name = x.Name?? null,
                                   Id = x.Id?? null
                               });
}
private IEnumerable From(IEnumerable doctorList)
{
returndoctorlist.Select(x=>newdoctor()
{
名称=x。名称??空,
Id=x.Id??空
});
}

您几乎是正确的,因为之前我有单独的属性,并使用了与您类似的select one。现在,我的上司让我使用一个对象和该对象的列表,这就是为什么我在挣扎。我现在正在尝试您的解决方案,将在几分钟内向您通报进展情况。幸运的是,您的解决方案给了我运行时错误。如果您告诉我错误是什么,我可以帮助您,您是否尝试删除
.Tostring()
是的,我完全按照您在此处给出的操作,但没有错误,它在运行时中断OK,我得到了与上面粘贴的相同的错误,但是,我确实得到了带有名称和ID的resultview。为什么仍然显示system.linq错误?该对象是什么?它不应该是
List
System.Linq.Enumerable+whereSelectEnumerableInterator
2`不是错误消息,而是类型名称。你能告诉我们错误信息是什么吗?@fubo:是的,不是医生list@LasseV.Karlsen:我没有看到错误,但我没有得到我想要的值,因此您有两个类用于doctor-
doctor
DoctorList