3

我正在尝试使以下工作。我有一个包含字段FirstNameLastNameDoctorId的表。我想使用 Linq to SQL 填充一个 .net ListBox。这是我找到并借用的:

在我称为 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>;
  }
}

该错误与“).ToList;”有关 线。错误是:

错误 1 ​​无法将方法组“ToList”转换为非委托类型“System.Collections.Generic.List”。您是否打算调用该方法?Q:\myapp\WaitList\App_Code\DALClass.cs 37 16 Q:\myapp\WaitList\

我不确定我是否应该把<String>而不是<Doctor>. 我已经尝试过了,但它不起作用。

4

4 回答 4

6

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

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

更新

要解决第二个错误(“ 'Doctor' 不包含'FullName' 的定义”),问题是您没有在Doctor.

您可以尝试在 上定义一个单独的属性Doctor,但我不确定 Linq-to-SQL 是否允许这样做。您还可以重用现有属性之一(例如LastName),但这听起来不是特别优雅。

我建议设计一个单独的实体(通常您会使用匿名类型来完成此操作,但由于您似乎是从方法返回它,因此如果您关心类型安全,这不是一个选项):

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();
于 2013-09-13T22:24:30.560 回答
3

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

).ToList<Doctor>();

其次,您将无法将匿名类型强制转换为Doctor类。你想要的是:

  return (from c in db.Doctors
            select new Doctor() // Create Doctor instance
            {
              FullName = c.FirstName + " " + c.LastName,
              DoctorId = c.DoctorId
            }
          ).ToList(); // No longer a need to specify type on generic method

更新:如果是我,我会更新Doctor类(无论如何自动生成的模型都是部分类)以具有FullName如下属性:

public partial class Doctor
{
   public string FullName // Read only FullName property
   {
      get
      {
         return this.FirstName + " " + this.LastName;
      }
   }
}

然后,您可以只返回您的Doctor对象列表(我假设db.DoctorsDoctor对象的集合):

  return (from c in db.Doctors select c).ToList();

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

于 2013-09-13T22:24:27.373 回答
2

我假设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
}
于 2013-09-13T22:38:29.933 回答
0

我相信您只是缺少括号,表明您正在尝试执行 ToList<>() 方法:)

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

}

于 2013-09-13T22:25:11.477 回答