C#:如何从 linq 查询设置组合框 valuemember
好的,所以我有组合框,其数据源是 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";
,我在运行时收到错误,它无法转换匿名类型。 我该如何解决这个问题?
更正: 错误是:
无法绑定到新值成员。 参数名称:值
ok so i have combobox whos datasource are the results of a linq query
//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";
The problem im having is when i try to add the next line of code
cboQASupervisor.ValueMember = "ID";
I get an error on runtime that it couldn't cast the anonymous type. How do i fix this?
Correction:
The error is:
Cannot bind to the new value member.
Parameter name: value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您指定 ID 作为值字段,但匿名类型中没有 ID 属性。
假设您的 LUT_Employees 对象中有 ID:
You specify ID as the value field, but you don't have ID property in your anonymous type.
Assuming you have ID in your LUT_Employees object:
您可以尝试以下操作:
将
.ToList()
添加到数据源行中的代码中。You can try this:
Add
.ToList()
to your code in the datasource line.