好吧所以我有组合框,其中数据源是linq查询的结果
//load QA names
var qaNames =
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { a, Names = a.lastName + ", " + a.firstName };
cboQASupervisor.DataSource = qaNames;
cboQASupervisor.DisplayMember = "Names";
我遇到的问题是当我尝试添加下一行代码时
cboQASupervisor.ValueMember = "ID";
我在运行时遇到错误它无法转换匿名类型。我该如何解决这个问题?
校正: 错误是:
无法绑定到新值成员。 参数名称:值
答案 0 :(得分:12)
您指定ID作为值字段,但您的匿名类型中没有ID属性 假设您的LUT_Employees对象中有ID:
var qaNames = (
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { a.ID, Names = a.lastName + ", " + a.firstName })
.ToList();
cboQASupervisor.DataSource = qaNames;
cboQASupervisor.DisplayMember = "Names";
cboQASupervisor.ValueMember = "ID";
答案 1 :(得分:2)
你可以试试这个:
var qaNames =
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { Id = a.ID, Names = a.lastName + ", " + a.firstName };
cboQASupervisor.DataSource = qaNames.ToList();
cboQASupervisor.DisplayMember = "Names";
cboQASupervisor.ValueMember = "Id";
将.ToList()
添加到数据源行的代码中。