Pregunta

OK, así que tengo ComboBox Whos DataSource son los resultados de una consulta 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";

El problema que estoy teniendo es cuando intento agregar la siguiente línea de código

cboQASupervisor.ValueMember = "ID";

Recibo un error en el tiempo de ejecución que no podía lanzar el tipo anónimo. ¿Cómo puedo arreglar esto?

Corrección: El error es:

no puede enlazar al nuevo miembro de valor. Nombre del parámetro: valor

¿Fue útil?

Solución

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:

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";

Otros consejos

You can try this:

       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";

Add .ToList() to your code in the datasource line.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top