5

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

我在运行时收到一个错误,它无法转换匿名类型。我该如何解决?

更正:错误是:

无法绑定到新值成员。参数名称:值

4

2 回答 2

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";
于 2011-05-18T03:22:22.153 回答
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()到数据源行中的代码。

于 2011-05-18T03:28:50.693 回答