连接 EF 中的两个属性以填充组合框
本文关键字:属性 两个 填充 组合 EF 连接 | 更新日期: 2023-09-27 18:33:51
我使用实体框架,C#和.NET 4.5开发一个新的WinForms项目。在我的数据库中,我有一个表格"老师",有两列"名字"和"姓氏"。如何使用这两个属性生成的值填充 ComboBox 控件?
场景:
private List<Teacher> ShowTeacher(String teacherID)
{
List<Teacher> teachers = (from teacher in context.Teacher
select teacher).Where(t => t.UserID == teacherID).ToList();
return teachers;
}
以及ComboBox中的代码片段:
cmbTeacher.DataSource = ShowTeacher(teacherID);
cmbTeacher.DisplayMember = ""; //here should be placed the string "FisrtName"+"LastName"
提前致谢
您需要在数据源中包含全名值
List<Teacher> teachers = (from teacher in context.Teacher
let FullName = teacher.FirstName + " " + teacher.LastName
select teacher).Where(t => t.UserID == teacherID).ToList();
cmbTeacher.DataSource = ShowTeacher(teacherID);
cmbTeacher.DisplayMember = "FullName";
class Teacher
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public string FullName
{
get
{
return LastName + " " + FirstName;
}
}
public Teacher(string firstname, string lastname)
{
FirstName = firstname;
LastName = lastname;
}
}