none
¿how do a join between datatable and Linq? RRS feed

  • Question

  • How are you?
    I have a Data and I want to make an internal link with a Linq query.

    Attached code that I am using but that returns to an error without support.

    int idEmpleado = 87;
    
    IEnumerable<C_Base_FP_Empleados_Provincias> resultadoAux1 = (from provinciasPermitidas in listaProvincias
                                             select provinciasPermitidas).AsEnumerable<C_Base_FP_Empleados_Provincias>();
    
                        IEnumerable <DataRow> resultadoAux2 = (from tabla in table.AsEnumerable()
                                             select tabla);
    
                        IEnumerable<DataRow> resultado = (from provinciasPermitidas in resultadoAux1
                                                          join tabla in resultadoAux2 on provinciasPermitidas.ID_Provincia_AP equals tabla.Field<int>("Id Provincia")
                                         where provinciasPermitidas.ID_Empleado == idEmpleado
                                         select tabla);
    

    c_Base_FP_Empleados_Provincias is a class of model Entity Framework .edmx.

    Thank you very much for the help you can give me or for confirming that this type of join can not be done.

    Thank you very much for the help you can give me or for confirming that this type of join can not be done.
    Thank you very much for the help you can give me or for confirming that this type of join can not be done.
    Tuesday, December 11, 2018 2:38 PM

Answers

  • Hi CrissandoCO,

    According to your description, I create a simple as below, it works well, could you please share a bit more information, such as related data source about datatable and listaProvincias.

    DataTable table = new DataTable("table1");
    table.Columns.Add("Id Provincia", typeof(int));
    table.Columns.Add("TestName", typeof(string));
    
                for (int i = 1; i <= 20; i++)
                {
                    DataRow row = table.NewRow();
                    row["Id Provincia"] = i;
                    row["TestName"] = string.Format("Test-{0}", i);
                    table.Rows.Add(row);
                }
    
    
    List<C_Base_FP_Empleados_Provincias> listaProvincias = new List<C_Base_FP_Empleados_Provincias>()
                {
                    new C_Base_FP_Empleados_Provincias(){ ID_Provincia_AP = 1, ID_Empleado = 87},
                    new C_Base_FP_Empleados_Provincias(){ ID_Provincia_AP = 2, ID_Empleado = 87},
                };
                int idEmpleado = 87;
    
                IEnumerable<C_Base_FP_Empleados_Provincias> resultadoAux1 = (from provinciasPermitidas in listaProvincias
                                                                             select provinciasPermitidas).AsEnumerable<C_Base_FP_Empleados_Provincias>();
    
    
    List<int> list = resultadoAux1.Where(t => t.ID_Empleado == idEmpleado).Select(x => x.ID_Provincia_AP).ToList();
    
    IEnumerable<DataRow> resultadoAux2 = (from tabla in table.AsEnumerable()
                                                      select tabla);
               
    IEnumerable<DataRow> resultado = (from provinciasPermitidas in resultadoAux1
                                                  join tabla in resultadoAux2 on provinciasPermitidas.ID_Provincia_AP equals tabla.Field<int>("Id Provincia")
                                                  where provinciasPermitidas.ID_Empleado == idEmpleado
                                                  select tabla);
    
    
    foreach (DataRow item in resultado)
    {
                    Console.WriteLine("{0} -- {1}", item[0], item[1]);
    }
    Console.ReadKey();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 12, 2018 1:51 AM
    Moderator