LinQ to Entities:查询四个表JOIN

时间:2009-03-21 05:54:58

标签: c# linq-to-entities

我有四张桌子:

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

客户可能有零或n个目的地。目的地有n个DestinationDetails,每个DestinationDetail都有一种语言。

确定。我需要检索给定客户端和给定语言的所有DestinationDetails。

我开始写这个:

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
   {
   }
}

有什么建议吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

它的界限:

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;

答案 1 :(得分:1)

你有没有尝试过linq连接:

Select from multiple table using LINQ

答案 2 :(得分:0)

我的回答:


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


谢谢!