linq to sql join on multiple columns using lambda

46,848

Solution 1

var query = context.ShoppingMalls
                   .Join(
                       context.Houses,
                       s => new { s.CouncilCode, s.PostCode },
                       h => new { h.CouncilCode, h.PostCode },
                       (s, h) => s);

Solution 2

Although the example and answer given by @Thomas Levesque works for columns that match, I wanted to also supply the answer if you have columns to join on but they have different names. This is what I needed for my googling and this question got me close.

The difference of course is the explicit declaration of the columns as a variable to identify on.

var query = context.MapKitsToResources
              .Join(
                     context.Resources, 
                     o => new { Id = o.ResourceId, Type = o.ResourceTypeId},
                     i => new { Id = i.Id, Type = TypeId},
                     (o, i) = new { rType : i };
Share:
46,848
spdro
Author by

spdro

Updated on May 01, 2020

Comments

  • spdro
    spdro almost 4 years

    Can someone help me to translate this

    var query = from s in context.ShoppingMalls
    join h in context.Houses
    on
    new { s.CouncilCode, s.PostCode }
    equals
     new { h.CouncilCode, h.PostCode }
    select s;
    

    into lambda query?

    Thanks.