show table data related to LINQ

0

Dear friends, good evening.

I have a method to fill clients by name in a datagridview this is my code:

    public List<CLIENTES> GetClientesByNombre(string nombre)
    {
        using (RSAEntities db = new RSAEntities())
        {
            //return db.CLIENTES.ToList();

            var Query = (from c in db.CLIENTES
                         where c.CLI_NOMBRE.StartsWith(nombre)
                         orderby c.CLI_NOMBRE
                         select c).ToList();

            return Query;

        }
    }

in my form I am filling in the datagridview manually.

    private void btnListar_Click(object sender, EventArgs e)
    {
        if (txtNombre.Text.ToString().Trim().Length==0)
        {
            MessageBox.Show("Ingrese un Nombre");
            txtNombre.Focus();
        }
        else
        {
            NCliente nCliente = new NCliente();
            List<CLIENTES> lista = new List<CLIENTES>();
            lista = nCliente.GetClientesByNombre(txtNombre.Text.ToString().Trim());
            dgvClientes.AutoGenerateColumns = false;
            dgvClientes.DataSource = lista;
        }

    }

in design mode I handle the columns ejm

How do I show the GRU_NOMBRE field of the GROUP entity that is related to the CLIENT entity?

Thank you all for your valuable help.

    
asked by Guivan 14.09.2017 в 07:15
source

1 answer

0

You must make sure to also include the information of the entity GROUP: Using the extension method .Include () that is in the namespace System.Data.Entity in the case of EntityFramework 6.0

public List<CLIENTES> GetClientesByNombre(string nombre)
{
    using (RSAEntities db = new RSAEntities())
    {
        //return db.CLIENTES.ToList();

        var Query = (from c in db.CLIENTES.Include(c => c.GRUPO)
                     where c.CLI_NOMBRE.StartsWith(nombre)
                     orderby c.CLI_NOMBRE
                     select c).ToList();

        return Query;

    }
}

Finally as it says @Pikoh try with GRUPO.GRU_NOMBRE in the DataGridView

    
answered by 15.09.2017 в 02:16