Linq to sql with subquery

Asked

Viewed 170 times

1

I have two tables, and would like to get only the data from table A, filtering by related table B. I would like to know how to write this query with Linq to Sql in C#. I wrote the example below in Sql Server to make it easier to understand.

    Declare @tabela_a as table (
        ID_A int, --PK
        nome varchar(20)
    )
    Declare @tabela_b as table (
        ID_B int, -- PK
        ID_A int, -- FK
        nome varchar(20)
    )
    insert into @tabela_a values (1, 'AAA')
    insert into @tabela_a values (2, 'BBB')
    insert into @tabela_a values (3, 'CCC')

    insert into @tabela_b values (1, 1, 'A1')
    insert into @tabela_b values (2, 1, 'A2')
    insert into @tabela_b values (3, 1, 'A3')
    insert into @tabela_b values (4, 2, 'B1')
    insert into @tabela_b values (5, 2, 'B2')
    insert into @tabela_b values (6, 2, 'B3')

-- Exemplo de Query que eu gostaria que seja executada
    SELECT * FROM @TABELA_A A
    WHERE EXISTS (
        SELECT NULL FROM @TABELA_B B 
        WHERE ID_A = A.ID_A 
        AND B.NOME IN ('A1', 'A2', 'B3')
    )

1 answer

2


tabelaA.Where(t1 => tabelaB.Any(t2 => t2.id.equals(t1.id) && (t2.nome.equals("A1") || t2.nome.equals("A2") || t2.nome.equals("B3"))).ToList()
  • @Rafael Marcos I answered your question? If yes, mark my answer as resolved.

Browser other questions tagged

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