linq到sql连接-列表框的FirstName和LastName

linq到sql连接-列表框的FirstName和LastName,linq,linq-to-sql,Linq,Linq To Sql,我正在努力让下面的内容发挥作用。我有一个表,其中包含字段FirstName、LastName和DoctorId。我想使用Linq to SQL填充.net列表框。以下是我发现并借用的: 在一个我称之为DALClass的类中: public List<Doctor> GetListDoctorsNames() { using (var db = new WaitListDataContext()) { return (from c in db.Doctors

我正在努力让下面的内容发挥作用。我有一个表,其中包含字段FirstNameLastNameDoctorId。我想使用Linq to SQL填充.net列表框。以下是我发现并借用的:

在一个我称之为DALClass的类中:

public List<Doctor> GetListDoctorsNames()
{
  using (var db = new WaitListDataContext())
  {
    return (from c in db.Doctors
            select new
            {
              FullName = c.FirstName + " " + c.LastName,
              DoctorId = c.DoctorId
            }
            ).ToList<Doctor>;
  }
}
公共列表getListDoctorNames()
{
使用(var db=new WaitListDataContext())
{
返回(从数据库中的c返回)
选择新的
{
全名=c.FirstName+“”+c.LastName,
DoctorId=c.DoctorId
}
)托利斯特先生;
}
}
错误与“.ToList;”行有关。错误是:

错误1无法将方法组“ToList”转换为非委托类型 “System.Collections.Generic.List”。你打算调用吗 这个 方法?Q:\myapp\WaitList\App\u Code\DALClass.cs 37 16 Q:\myapp\WaitList\


我不确定我是否应该把
而不是
。我已经试过了,但没有成功。

第一个问题,
ToList()
是一个方法,需要作为一个方法调用:

).ToList<Doctor>();
更新:如果是我,我会更新
Doctor
类(自动生成的模型无论如何都是部分类),使其具有
FullName
属性,如下所示:

public partial class Doctor
{
   public string FullName // Read only FullName property
   {
      get
      {
         return this.FirstName + " " + this.LastName;
      }
   }
}
然后,您可以返回
Doctor
对象列表(我假设
db.Doctors
Doctor
对象的集合):


并直接绑定到对象的
FullName
属性。

您的查询返回的是匿名类型,并且您实际上没有调用
ToList
方法。您可能需要指定数据类型select子句,并使用括号调用
ToList
方法,如下所示:

return 
    (from c in db.Doctors
     select new Doctor
     {
        FullName = c.FirstName + " " + c.LastName,
        DoctorId = c.DoctorId
     })
    .ToList<Doctor>(); // or just .ToList();

我相信您只是缺少了表示您正在尝试执行ToList()方法的括号:)

使用(var db=new WaitListDataContext())
{
返回(从数据库中的c返回)
选择新的
{
全名=c.FirstName+“”+c.LastName,
DoctorId=c.DoctorId
}
).ToList();

}

我假设
Doctor
类实际上是数据库上下文中的一个实体。在这种情况下,您需要创建自己的类型并返回该类型,而不是
Doctor

public List<MyDoctor> GetListDoctorsNames()
{
  using (var db = new WaitListDataContext())
  {
      return db.Doctors.Select(c => new MyDoctor()
             {
                 FullName = c.FirstName + " " + c.LastName,
                 DoctorId = c.DoctorId
             }).ToList();
  }
}

public class MyDoctor
{
  public string FullName {get; set;} 
  public int DoctorId {get; set;} //Or what ever type DoctorId is
}
公共列表getListDoctorNames()
{
使用(var db=new WaitListDataContext())
{
返回db.documents.Select(c=>newmydoctor()
{
全名=c.FirstName+“”+c.LastName,
DoctorId=c.DoctorId
}).ToList();
}
}
公立医生
{
公共字符串全名{get;set;}
public int DoctorId{get;set;}//或DoctorId的任何类型
}

试试
.ToList()似乎更近了。现在错误显示为:“错误1‘医生’不包含‘全名’的定义”。Select语句是否不正确?必须关闭。但是,错误是,“'Doctor'不包含'FullName'的定义”。谢谢!就这样。我没有“全名”属性。创建一个只包含全名和id的类是完美的。非常感谢你的帮助!我没有“全名”属性。我只是为了解决这个问题而编造了一个计划。医生没有“全名”。我是否应该尝试其他方法来解决用医生全名填充列表框的原始问题?你是对的!我需要一个类来保存属性FullName,因为我的linqtosql类没有。非常感谢你。我从你的代码中学到了很多。
return 
    (from c in db.Doctors
     select new Doctor
     {
        FullName = c.FirstName + " " + c.LastName,
        DoctorId = c.DoctorId
     })
    .ToList<Doctor>(); // or just .ToList();
public class DisplayDoctor
{
    public string FullName { get; set; }
    public int DoctorId { get; set; }
}

return 
    (from c in db.Doctors
     select new DisplayDoctor
     {
        FullName = c.FirstName + " " + c.LastName,
        DoctorId = c.DoctorId
     })
    .ToList<DisplayDoctor>(); // or just .ToList();
using (var db = new WaitListDataContext())
{
  return (from c in db.Doctors
        select new
        {
          FullName = c.FirstName + " " + c.LastName,
          DoctorId = c.DoctorId
        }
      ).ToList<Doctor>();
public List<MyDoctor> GetListDoctorsNames()
{
  using (var db = new WaitListDataContext())
  {
      return db.Doctors.Select(c => new MyDoctor()
             {
                 FullName = c.FirstName + " " + c.LastName,
                 DoctorId = c.DoctorId
             }).ToList();
  }
}

public class MyDoctor
{
  public string FullName {get; set;} 
  public int DoctorId {get; set;} //Or what ever type DoctorId is
}