152

Given:

A table named TABLE_1 with the following columns:

  • ID
  • ColumnA
  • ColumnB
  • ColumnC

I have SQL query where TABLE_1 joins on itself twice based off of ColumnA, ColumnB, ColumnC. The query might look something like this:

Select t1.ID, t2.ID, t3.ID
  From TABLE_1 t1
  Left Join TABLE_1 t2 On
       t1.ColumnA = t2.ColumnA
   And t1.ColumnB = t2.ColumnB
   And t1.ColumnC = t2.ColumnC
  Left Join TABLE_1 t3 On
       t2.ColumnA = t3.ColumnA
   And t2.ColumnB = t3.ColumnB
   And t2.ColumnC = t3.ColumnC
... and query continues on etc.

Problem:

I need that Query to be rewritten in LINQ. I've tried taking a stab at it:

var query =
    from t1 in myTABLE1List // List<TABLE_1>
    join t2 in myTABLE1List
      on t1.ColumnA equals t2.ColumnA
      && t1.ColumnB equals t2.ColumnA
    // ... and at this point intellisense is making it very obvious
    // I am doing something wrong :(

How do I write my query in LINQ? What am I doing wrong?

aarona
  • 35,986
  • 41
  • 138
  • 186

8 Answers8

287

Joining on multiple columns in Linq to SQL is a little different.

var query =
    from t1 in myTABLE1List // List<TABLE_1>
    join t2 in myTABLE1List
      on new { t1.ColumnA, t1.ColumnB } equals new { t2.ColumnA, t2.ColumnB }
    ...

You have to take advantage of anonymous types and compose a type for the multiple columns you wish to compare against.

This seems confusing at first but once you get acquainted with the way the SQL is composed from the expressions it will make a lot more sense, under the covers this will generate the type of join you are looking for.

EDIT Adding example for second join based on comment.

var query =
    from t1 in myTABLE1List // List<TABLE_1>
    join t2 in myTABLE1List
      on new { A = t1.ColumnA, B = t1.ColumnB } equals new { A = t2.ColumnA, B = t2.ColumnB }
    join t3 in myTABLE1List
      on new { A = t2.ColumnA, B =  t2.ColumnB } equals new { A = t3.ColumnA, B = t3.ColumnB }
    ...
AceMark
  • 701
  • 1
  • 11
  • 21
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • 4
    this works great for two joins. I need it to work with THREE joins. Sorry, the second code block was a little misleading. – aarona Mar 15 '11 at 05:31
  • 62
    If you get a compiler error about type inference, check two things, (1) are the types the same, and (2) are the column names the same. The names part is a gotcha. This example won't compile even if all columns are varchars `join T2 in db.tbl2 on new { T1.firstName, T1.secondName } equals new { T2.colFirst, T2.colSecond }`. If you change it to this, it will compile however, `join T2 in db.tbl2 on new { N1 = T1.firstName, N2 = T1.secondName } equals new { N1 = T2.colFirst, N2 = T2.colSecond }` – user2023861 Oct 03 '13 at 13:56
  • 4
    Naming problem can be eliminated by from t1 in myTABLE1List join t2 in myTABLE1List on new { colA=t1.ColumnA, colB=t1.ColumnB } equals new { colA=t2.ColumnA, colBBt2.ColumnB } – Baqer Naqvi Oct 21 '14 at 14:13
  • 1
    please allow me to edit the example, as it needed assignments to anonymous properties – AceMark Jul 24 '15 at 06:14
  • 1
    Something is wrong here.. with LINQ. I can join on multiple tables, I can join on multiple fields... however, I can not do it for both, as the example shows here. So say you just have a join on 1 field.. and you have a 2nd join following it. If you change the 1st join (or both) to just use new { x.field } equals new { y.field } there is a compiler error. Functionally you didn't change anything. Using .Net 4.6.1. – user2415376 Oct 28 '16 at 13:59
  • So it does work! but I was not setting the variables like "A = " or "B = ".. without that, having multiple joins would not work. – user2415376 Oct 28 '16 at 14:15
  • I need to do something like A=lr.SentFromEmployeeId || lr.SentToEmployeeId where the joining table A=e1.id has an or relationship – Golden Lion Nov 15 '21 at 21:59
15

U can also use :

var query =
    from t1 in myTABLE1List 
    join t2 in myTABLE1List
      on new { ColA=t1.ColumnA, ColB=t1.ColumnB } equals new { ColA=t2.ColumnA, ColB=t2.ColumnB }
    join t3 in myTABLE1List
      on new {ColC=t2.ColumnA, ColD=t2.ColumnB } equals new { ColC=t3.ColumnA, ColD=t3.ColumnB }
Baqer Naqvi
  • 6,011
  • 3
  • 50
  • 68
  • 6
    AHHH!! This works! And the KEY DIFFERENCE, is that you need to do the "ColA = " part so that in the other join it's same field. For years I didn't do that, but also would just need 1 join on multiple fields. But now I need more, and it ONLY WORKS if I assign a variable name to the fields like in this example. – user2415376 Oct 28 '16 at 14:14
13

In LINQ2SQL you seldom need to join explicitly when using inner joins.

If you have proper foreign key relationships in your database you will automatically get a relation in the LINQ designer (if not you can create a relation manually in the designer, although you should really have proper relations in your database)

parent-child relation

Then you can just access related tables with the "dot-notation"

var q = from child in context.Childs
        where child.Parent.col2 == 4
        select new
        {
            childCol1 = child.col1,
            parentCol1 = child.Parent.col1,
        };

will generate the query

SELECT [t0].[col1] AS [childCol1], [t1].[col1] AS [parentCol1]
FROM [dbo].[Child] AS [t0]
INNER JOIN [dbo].[Parent] AS [t1] ON ([t1].[col1] = [t0].[col1]) AND ([t1].[col2] = [t0].[col2])
WHERE [t1].[col2] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [4]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1

In my opinion this is much more readable and lets you concentrate on your special conditions and not the actual mechanics of the join.

Edit
This is of course only applicable when you want to join in the line with our database model. If you want to join "outside the model" you need to resort to manual joins as in the answer from Quintin Robinson

Community
  • 1
  • 1
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
12

Title_Authors is a look up two things join at a time project results and continue chaining

        DataClasses1DataContext db = new DataClasses1DataContext();
        var queryresults = from a in db.Authors                                          
                    join ba in db.Title_Authors                           
                    on a.Au_ID equals ba.Au_ID into idAuthor
                    from c in idAuthor
                    join t in db.Titles  
                    on c.ISBN equals t.ISBN 
                    select new { Author = a.Author1,Title= t.Title1 };

        foreach (var item in queryresults)
        {
            MessageBox.Show(item.Author);
            MessageBox.Show(item.Title);
            return;
        }
BionicCyborg
  • 121
  • 1
  • 2
4

I would like to give another example in which multiple (3) joins are used.

 DataClasses1DataContext ctx = new DataClasses1DataContext();

        var Owners = ctx.OwnerMasters;
        var Category = ctx.CategoryMasters;
        var Status = ctx.StatusMasters;
        var Tasks = ctx.TaskMasters;

        var xyz = from t in Tasks
                  join c in Category
                  on t.TaskCategory equals c.CategoryID
                  join s in Status
                  on t.TaskStatus equals s.StatusID
                  join o in Owners
                  on t.TaskOwner equals o.OwnerID
                  select new
                  {
                      t.TaskID,
                      t.TaskShortDescription,
                      c.CategoryName,
                      s.StatusName,
                      o.OwnerName
                  };
T.S.
  • 18,195
  • 11
  • 58
  • 78
user3477428
  • 101
  • 3
4

You can use LINQ Method Syntax to join on multiple columns. It's an example here,

var query = mTABLE_1.Join( // mTABLE_1 is a List<TABLE_1>
                mTABLE_1, 
                t1 => new
                {
                    ColA = t1.ColumnA,
                    ColB = t1.ColumnB,
                    ColC = t1.ColumnC
                },
                t2 => new
                {
                    ColA = t2.ColumnA,
                    ColB = t2.ColumnB,
                    ColC = t2.ColumnC
                },
                (t1, t2) => new { t1, t2 }).Join(
                mTABLE_1,
                t1t2 => new
                {
                    ColA = t1t2.t2.ColumnA,
                    ColB = t1t2.t2.ColumnB,
                    ColC = t1t2.t2.ColumnC
                },
                t3 => new
                {
                    ColA = t3.ColumnA,
                    ColB = t3.ColumnB,
                    ColC = t3.ColumnC
                },
                (t1t2, t3) => new
                {
                    t1 = t1t2.t1,
                    t2 = t1t2.t2,
                    t3 = t3
                });

Note: The compiler converts query syntax into method syntax at compile time.

2

You can also join if the number of columns are not same in both tables and can map static value to table column

from t1 in Table1 
join t2 in Table2 
on new {X = t1.Column1, Y = 0 } on new {X = t2.Column1, Y = t2.Column2 }
select new {t1, t2}
Ankit Arya
  • 872
  • 7
  • 10
0

The A and B alias must line up with Hrco and Position code from e table and t table - Hrco and Position Code combinations in the "equal new" filter. This will save you time because I kept getting "Not in scope on the left side" compile errors because I thought the filter was e.Hrco, t.Hrco pairing for the filter.

select * from table1 e
   join table2 t on
      e.Hrco=t.Hrco and e.PositionCode=t.PositionCode

   Notice the association of the columns to the labels A and B. The As equal and the Bs equal filter.

   IList<MyView> list = await (from e in _dbContext.table1
                                              join t in _dbContext.table2
                                              on new { A= e.Hrco, B= e.PositionCode }
                                              equals new {A= t.Hrco,B=t.PositionCode }
                                              where e.XMan == employeeNumber

                                              select new MyView
                                                                  { 
                                                                        
         Employee=e.Employee,
         LastName=e.LastName,
         FirstName=e.FirstName,
         Title=t.JobTitle
         ).ToListAsync<MyView>();
Golden Lion
  • 3,840
  • 2
  • 26
  • 35