1

鉴于这种:

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

如何返回按名字排序的列表?

4

3 回答 3

1
public static List<DoctorFullName> GetListDoctorsNames()
  {
  using (var db = new WaitListDataContext())
  {
     return db.Doctors.OrderBy(doc => doc.FirstName).Select(c => new DoctorFullName()
      {
        FullName = c.FirstName + " " + c.LastName,
        DoctorId = c.DoctorId
      }).ToList();
  }
}
于 2013-09-16T17:25:57.287 回答
0

您应该能够进行订购:

public static List<DoctorFullName> GetListDoctorsNames()
  {
  using (var db = new WaitListDataContext())
  {    
     return db.Doctors
              .OrderBy(d => d.FirstName)
              .Select(c => new DoctorFullName()
                  {
                    FullName = c.FirstName + " " + c.LastName,
                    DoctorId = c.DoctorId
                  })
              .ToList();
  }
}
于 2013-09-16T17:27:23.533 回答
0

只需添加一个.OrderBy()子句。您只能按FirstName之前的排序.Select()

return db.Doctors
         .OrderBy(c => c.FirstName)
         .Select(c => new DoctorFullName()
  {
    FullName = c.FirstName + " " + c.LastName,
    DoctorId = c.DoctorId
  }).ToList();

或者您可以在 之后对其进行排序,因为您的新字段无论如何.Select()都以该值开头:FirstName

return db.Doctors.Select(c => new DoctorFullName()
  {
    FullName = c.FirstName + " " + c.LastName,
    DoctorId = c.DoctorId
  })
  .OrderBy(c => c.FullName)
  .ToList();

IEnumerable<T>接口提供了许多用于操作集合和返回修改后的集合的方法,因此它们可以以多种不同的方式链接在一起。

于 2013-09-16T17:28:03.097 回答