0

I am trying to write a Linq query to generate the following SQL

SELECT 
    [t1].[Id], [t2].[value3]
FROM 
    [Table1] AS [t1]
INNER JOIN 
    [Table2] AS [t2] ON [t1].[Id] = [t2].[value1]
                     OR [t1].[Id] = [t2].[value2]

I have seen lots of examples for how to do multiple joins, but none for how to do this type of "one or the other" join.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3776749
  • 667
  • 1
  • 10
  • 20
  • Perhaps my [SQL to LINQ Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) might help you. – NetMage Sep 17 '21 at 19:28

2 Answers2

1
var result = from t1 in context.Table1
             from t2 in context.Table2
             where (t1.Id == t2.value1 || t1.Id == t2.value2)
             select new 
                    { 
                        t1.Id,
                        t2.value3
                    };
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nutzs
  • 79
  • 5
0

INNER JOIN

var query = 
    from t1 in context.Table1
    from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
    select new 
    { 
        t1.Id,
        t2.value3
    };

LEFT JOIN


var query = 
    from t1 in context.Table1
    from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
       .DefaultIfEmpty()
    select new 
    { 
        t1.Id,
        t2.value3
    };
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32