OR in Join using Linq

Asked

Viewed 104 times

3

I have the following tables:

Tabela1                Tabela2                               Tabela3                               Tabela4
+----+-----------+     +----+-----------+------+------+      +----+-----------+------+------+      +----+-----------+
| Id | Descricao |     | Id | Descricao | T1Id | T4Id |      | Id | Descricao | T1Id | T4Id |      | Id | Descricao |
+----+-----------+     +----+-----------+------+------+      +----+-----------+------+------+      +----+-----------+
| 1  | Item1     |     | 1  | Item1     |   1  |   1  |      | 1  | Item1     |   2  |   2  |      | 1  | Item1     |
| 2  | Item2     |     | 2  | Item2     |   1  |   2  |      | 2  | Item2     |   2  |   2  |      | 2  | Item2     |
+----+-----------+     | 3  | Item3     |   2  |   2  |      | 3  | Item3     |   1  |   1  |      | 3  | Item3     |
                       +----+-----------+------+------+      +----+-----------+------+------+      +----+-----------+


Both in the Table2 how much in the Table3, the column T4id is a foreign key of the column Id of Table4.

I have the following:

from t1 in db.Tabela1
join t2 in db.Tabela2 on t1.Id equals t2.T1Id
join t3 in db.Tabela3 on t1.Id equals t3.T1Id
join t4 in db.Tabela4 on ((t2.T4Id equals t4.TId) || (t3.T4Id equals t4.TId))
where t1.Id == 1
select t4;

I need to get the data from Table4 based on items from t2 and T3, but cannot use the OR (||) the way I’ve been doing on the 4th line.

I’m using Entity Framework.

Could someone help me?

  • Do you really need to do all these joins? Or the query can be rewritten using another approach?

  • Unfortunately I do. But what would that other approach be?

  • Don’t do the Join, start the query by Table 4 and not by Table 1.

  • got what he wanted?

1 answer

1

I made an example using classes to show how the Line (with lambda) should be done. You can change the && for || if you want the result to be in one of the tables, but not necessarily in both.

class Program
{
    static void Main(string[] args)
    {
        var tabela1 = new List<Tabela1>();
        var tabela2 = new List<Tabela2>();
        var tabela3 = new List<Tabela3>();
        var tabela4 = new List<Tabela4>();

        var query = tabela4.Where(x => 
            tabela2.Any(y => y.Tabela4 == x) && 
            tabela3.Any(y => y.Tabela4 == x));
    }
}

public class Tabela1
{
    public int Id { get; set; }
    public string Descricao { get; set; }
}

public class Tabela2
{
    public int Id { get; set; }
    public string Descricao { get; set; }
    public Tabela1 Tabela1 { get; set; }
    public Tabela4 Tabela4 { get; set; }
}

public class Tabela3
{
    public int Id { get; set; }
    public string Descricao { get; set; }
    public Tabela1 Tabela1 { get; set; }
    public Tabela4 Tabela4 { get; set; }
}

public class Tabela4
{
    public int Id { get; set; }
    public string Descricao { get; set; }
}

Browser other questions tagged

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