VansFannel
VansFannel

Reputation: 45921

Ado.Net entity framework, linq: select multiples tables

I use these sentence in C# to retrieve data from tables DetalleContenido and Archivo:

var detallesContenido =
   from contenido in guiaContext.Contenido
      where contenido.PuntoInteres.id_punto == puntoInteresID
   from dc in contenido.DetalleContenido
      where dc.Idioma.ds_idioma == idiomaCliente
  select dc;

The relationship between tables is this:

DataBase Model

I use puntoInteresID and idiomaCliente to retrieve all rows from DetalleContenido and Archivo that are part of PuntoInteres but whith this sentence Archivo is allways null!!

The sql sentece equivalence is:

Select dc.ds_nomDetContenido, dc.ds_descDetContenido, ar.archivo
from Contenido c, DetalleContenido dc, Archivo ar, Idioma i
where c.id_punto = puntoInteresID
  and c.id_contenido = dc.id_contenido
  and dc.id_idioma = i.id_idioma
  and i.ds_idioma = idiomaCliente
  and dc.id_archivo = ar.id_archivo;

How can I retrieve Archivo too?

Thank you!

Upvotes: 2

Views: 5206

Answers (4)

VansFannel
VansFannel

Reputation: 45921

My solution:

var detallesContenido =
   from contenido in guiaContext.Contenido
      where contenido.PuntoInteres.id_punto == puntoInteresID
   from detalleContenido in contenido.DetalleContenido
      where detalleContenido.Idioma.ds_idioma == idiomaCliente
   select new { detalleContenido, detalleContenido.Archivo };

Thank you!

Upvotes: 0

Tion
Tion

Reputation: 1490

how about:

var detallesContenido =
  from contenido in guiaContext.Contenido
      where contenido.PuntoInteres.id_punto == puntoInteresID && contenido.DetalleContenido.Any(c=>c.Idioma.ds_idioma == idiomaCliente)
  select contenido.DetalleContenido;

Upvotes: 0

JasonRShaver
JasonRShaver

Reputation: 4394

You can also do

.Load()

on the "reference" properity of the selected object (when you are using them).

But as for your query, it seems your Archivo table is a 1 to many and you cannot "include" or "load" the "many" side of the equasion. You would have to do a "select dc.Archivo" I would think.

Upvotes: 1

Tion
Tion

Reputation: 1490

have you tried using the Include() statement? I think it would look something like this

var detallesContenido =
       from contenido in guiaContext.Contenido.Include("DetalleContenido")
          where contenido.PuntoInteres.id_punto == puntoInteresID
       from dc in contenido.DetalleContenido
          where dc.Idioma.ds_idioma == idiomaCliente
      select dc;

Upvotes: 0

Related Questions