How to add one more condition in a query using Linux?

Asked

Viewed 499 times

3

How can I add one more condition to a Join ?

I have the consultation below;

 var consultaVeiculo = (from e in dm.Tabela_SegVeiculos
                                       join r in dm.Tabela_VeiculoRefers
                                       on  e.Modelo equals r.Cod_referencia 
                                       into veicRef
                                       from Referencia in veicRef.DefaultIfEmpty()
                                       where e.Seguradora == idSeguradora                                       
                                       orderby e.Modelo_cia
                                       select new { Descricao = Referencia == null ? "" : Referencia.Descricao, e });

It generates the following query in sql server:

SELECT 
    (CASE 
        WHEN [t2].[test] IS NULL THEN CONVERT(NVarChar(50),'')
        ELSE CONVERT(NVarChar(50),[t2].[Descricao])
     END) AS [Descricao], [t0].[Seguradora], [t0].[Veic_cia], [t0].[Fabricante], [t0].[Modelo_cia], [t0].[Modelo], [t0].[Categ_AT], [t0].[Situacao], [t0].[Data_inclusao], [t0].[Data_alteracao], [t0].[COD_FIPE]
FROM [dbo].[Tabela_SegVeiculos] AS [t0]
LEFT OUTER JOIN (
    SELECT 1 AS [test], [t1].[Descricao], [t1].[Cod_referencia]
    FROM [dbo].[Tabela_VeiculoRefer] AS [t1]
    ) AS [t2] ON [t0].[Modelo] = ([t2].[Cod_referencia])
WHERE ([t0].[Seguradora] = 83) AND ([t2].[Cod_referencia] > 0)
ORDER BY [t0].[Modelo_cia]

I need it right after the ON [t0].[Modelo] = ([t2].[Cod_referencia]) be added the and [t2].[Cod_referencia] > 0.

I have tried to do this the way below, but as shown in the image it displays an error.

inserir a descrição da imagem aqui

2 answers

5


For this case, do not use Join. Use in the Where. Linq is not SQL:

var consultaVeiculo = (from e in dm.Tabela_SegVeiculos
                                   join r in dm.Tabela_VeiculoRefers
                                   on e.Modelo equals r.Cod_referencia 
                                   into veicRef
                                   from Referencia in veicRef.DefaultIfEmpty()
                                   where e.Seguradora == idSeguradora && r.Cod_referencia > 0                                      
                                   orderby e.Modelo_cia
                                   select new { Descricao = Referencia == null ? "" : Referencia.Descricao, e });

Or even, according to the author’s problem:

var consultaVeiculo = (from e in dm.Tabela_SegVeiculos
                                   join r in (from ri 
                                              in dm.Tabela_VeiculoRefers 
                                              where ri.Cod_referencia > 0 
                                              select ri)
                                   on e.Modelo equals r.Cod_referencia 
                                   into veicRef
                                   from Referencia in veicRef.DefaultIfEmpty()
                                   where e.Seguradora == idSeguradora && r.Cod_referencia > 0                                      
                                   orderby e.Modelo_cia
                                   select new { Descricao = Referencia == null ? "" : Referencia.Descricao, e });
  • I’ve tried that, only the query causes Cod_reference > 0 to be right at the end , I need something that is after the ON.

  • Why? What changes in the end?

  • 1

    I only needed to change the query to use sub queries Join r in (from ri in dm.Tabela_veiculorefers Where ri.Cod_reference > 0 select ri)

3

 var consultaVeiculo = (from e in dm.Tabela_SegVeiculos
            join r in dm.Tabela_VeiculoRefers
            on new { id = e.Modelo, verdadeiro = true } equals new { id = r.Cod_referencia, verdadeiro = r.Cod_referencia > 0}
            into veicRef
            from Referencia in veicRef.DefaultIfEmpty()
            where e.Seguradora == idSeguradora
            orderby e.Modelo_cia
            select new { Descricao = Referencia == null ? "" : Referencia.Descricao, e });
  • 1

    I can’t understand where your answer fits the question. A tip: you don’t need to put your code as snippet, you can just play the text in the post and use Ctrl + K to format it.

  • Returns (invalid Anonymous type Member declarator)

  • I did the test: I put names in the variables of the anonymous object. it worked.

Browser other questions tagged

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