VansFannel
VansFannel

Reputation: 45911

LinQ to Entities: Query with four tables JOIN

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!

Upvotes: 2

Views: 12839

Answers (3)

VansFannel
VansFannel

Reputation: 45911

My answer:


var db = new PracticeEntities();
            var destinations = db.DestinationDetails.
                Where(dd => dd.Language.Lang == "en-US" &&
                dd.Destination.Client.Any(c => c.Email == "[email protected]"));


Thanks!

Upvotes: 0

eglasius
eglasius

Reputation: 36027

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;

Upvotes: 2

Jonathan Parker
Jonathan Parker

Reputation: 6795

Have you tried doing linq joins:

Select from multiple table using LINQ

Upvotes: 1

Related Questions