Tutorial :LinQ to Entities: Query with four tables JOIN



Question:

I have four tables:

- Client with PK ClientID.  - Destination with PK DestinationID.  - Language with PK LanguageID.  - DestinationDetail with PK DestinationID.  - RL-Client-Destination with PKs ClientID and DestinationID.  

The Client may have zero or n Destinations. A destination has n DestinationDetails, each of these DestinationDetail has a language.

Ok. I need to retrieve all of DestinationDetails for a given client and a given language.

I start writing this:

  try     {        ObjectQuery clientes =             guiaContext.Cliente;        ObjectQuery destinos =            guiaContext.Destino;        ObjectQuery idiomas =            guiaContext.Idioma;        ObjectQuery detalles =            guiaContext.DetalleDestino;          IQueryable detalleQuery =            from cliente in clientes            from destino in destinos            from idioma in idiomas            from detalleDestino in detalles            where destino.            select detalleDestino;       }     catch     {     }  }    

Any advice?

Thanks!


Solution:1

Its along the lines:

var detalleQuery =            from client in guiaContext.Clients            where client.ID == 1            from destination in cliente.Destinations            from destinationDetail in destination.DestionationDetails            where destinationDetail.Language.Iso2Code == "es"            select destinationDetail;  


Solution:2

Have you tried doing linq joins:

Select from multiple table using LINQ


Solution:3

My answer:

    var db = new PracticeEntities();              var destinations = db.DestinationDetails.                  Where(dd => dd.Language.Lang == "en-US" &&                  dd.Destination.Client.Any(c => c.Email == "abc@yahoo.com"));      

Thanks!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »