好的,所以我有组合框,其数据源是 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";
我在运行时遇到错误,它无法转换匿名类型。我该如何解决这个问题?
更正: 错误是:
Cannot bind to the new value member. Parameter name: value
最佳答案
您将 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";
关于c#:如何从 linq 查询中设置组合框值成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6039501/