gpt4 book ai didi

c#:如何从 linq 查询中设置组合框值成员

转载 作者:太空狗 更新时间:2023-10-29 22:14:47 25 4
gpt4 key购买 nike

好的,所以我有组合框,其数据源是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com