Query with LINQ, trying to perform a RIGHT JOIN

Asked

Viewed 59 times

1

I need to make the following query through LINQ, using the informed tables:

Must return all vehicles where the "Type" has the field "PART" equal to "true", the link of the table "Vehicle" with the table "Type" is made through the table "Model", however if the vehicle has already been linked to the table "Set" it also should not be displayed.

In this case I need to return the vehicles with ID 51, 66 and 72

Veiculo
-----------------
ID PLACA   IDMODELO
49 AAA0001 13
50 AAA0002 13
51 AAA0003 15
65 AAA0004 19
66 AAA0005 14
71 AAA0006 13
72 AAA0007 19


Modelo
-----------------
ID MODELO   IDTIPO
13 Reboque  22
14 Caminhão 27
15 Carreta  28
19 Bitrem   27


Tipo
-----------------
ID TIPO PARTE
22 V1   false
27 V2   true
28 R1   true


Conjunto
-----------------
IDVEICULOCONJUNTO IDVEICULO
50                65

I tried to perform the following consultation:

from vei in dc.Veiculo
join mod in dc.ModeloVeiculo on vei.IdModeloVeiculo equals mod.IdModeloVeiculo
join tiv in dc.TipoVeiculo on mod.IdTipoVeiculo equals tiv.IdTipoVeiculo
join vec in dc.VeiculoConjunto on vei.IdVeiculo equals vec.IdVeiculo into jData
from jvei in jData.DefaultIfEmpty()
where tiv.ParteConjunto.Equals(true) &&
    (jvei == null || (idVeiculo == 0 || jvei.IdVeiculo == idVeiculo))
select vei

but he returns Ids 51, 65, 65 and 72

  • 2

    You already tried to do something?

  • 1

    What have you done so far? Making a mistake? Which one?

  • I updated the question with what I had tested

1 answer

2


I resolved the matter as follows:

from vei in dc.Veiculo
join mod in dc.ModeloVeiculo on vei.IdModeloVeiculo equals mod.IdModeloVeiculo
join tiv in dc.TipoVeiculo on mod.IdTipoVeiculo equals tiv.IdTipoVeiculo
join vec in dc.VeiculoConjunto on vei.IdVeiculo equals vec.IdVeiculo into jData
from jvei in jData.DefaultIfEmpty()
where tiv.ParteConjunto.Equals(true) &&
    (jvei == null || (idVeiculo != 0 && jvei.IdVeiculo == idVeiculo))
select vei

Browser other questions tagged

You are not signed in. Login or sign up in order to post.