Home / Programming / Blog article: LINQ To SQL Join On Multiple Conditions

| RSS

LINQ To SQL Join On Multiple Conditions

September 16th, 2008 | 11 Comments | Posted in Programming

LINQ To SQL or just plain LINQ allows us to do a join on multiple conditions using an elegant technique. In this post I will show you how to perform a Join on multiple conditions. As an example I will take two tables called House and ShoppingMall.

image

Both these tables have PostCode and CouncilCode as common fields. Lets say that we want to retrieve all records from ShoppingMall where both PostCode and CouncilCode on House match. This requires us to do a join using two columns. In LINQ such a join can be done using anonymous types. Here is an example.

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;


The code above gets translated into this SQL query.

SELECT [t0].[ShoppingMallId], [t0].[Address],
[t0].[PostCode], [t0].[CouncilCode]
FROM [dbo].[ShoppingMall] AS [t0]
INNER JOIN [dbo].[House] AS [t1]
ON ([t0].[CouncilCode] = [t1].[CouncilCode])
AND ([t0].[PostCode] = [t1].[PostCode])

And when the above query is executed it produces the results we want.






Leave a Reply 34618 views, 154 so far today |
Tags:
Follow Discussion

11 Responses to “LINQ To SQL Join On Multiple Conditions”

  1. loopGhost Says:

    Awesome… bump, so hopefully this will get a little google traction.

  2. jwright Says:

    something to keep in mind.
    if your joining two tables where the linking columns have different names, the anonymous types’ properties must match.
    i.e.,

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

  3. Deepak Says:

    Thanks for the tip jwright.

  4. mp09ind Says:

    What is context here?

  5. Deepak Says:

    mp09ind,

    context is my DataContext here which I generated using Visual Studio designer. You can find more details on how to do that here.

    http://www.onedotnetway.com/linq-to-sql-tutorial/

  6. mike313t Says:

    What if when joining one condition is an “equals”
    and another condition is “not equals”?

  7. IanF Says:

    Hi this is great unfortunatley it results in an ‘OR’ for me and not an AND. i.e.:

    SELECT [t0].[ShoppingMallId], [t0].[Address],[t0].[PostCode], [t0].[CouncilCode]FROM [dbo].[ShoppingMall] AS [t0]INNER JOIN [dbo].[House] AS [t1]ON ([t0].[CouncilCode] = [t1].[CouncilCode])OR ([t0].[PostCode] = [t1].[PostCode])

  8. Deepak Says:

    Ian,

    What does your LINQ query look like? Can you post it here?

  9. IanF Says:

    I can include a little bit, well the bit that is the offending part.

    join E in bfd.BFDStateMachineTypeState
    on new { D.StateMachineTypeID, A.StateID
    equals new { E.StateMachineTypeID, E.StateID }
    join L in bfd.BFDStateMachineTypeService
    on new { D.StateMachineTypeID, H.ServiceID }
    equals new { L.StateMachineTypeID, L.ServiceID }

    Thanks,

    Ian

  10. IanF Says:

    Sorry ignore me, it is not the join that is causing the OR condition. I have run Profiler and I am getting superfluos null tests like this:

    INNER JOIN [dbo].[BFDStateMachineTypeState] AS [Extent8] ON (([Extent7].[StateMachineTypeID] = [Extent8].[StateMachineTypeID]) [b]OR (([Extent7].[StateMachineTypeID] IS NULL) AND ([Extent8].[StateMachineTypeID] IS NULL)))[/b] AND (([Extent6].[StateID] = [Extent8].[StateID]) [b]OR (([Extent6].[StateID] IS NULL) AND ([Extent8].[StateID] IS NULL)))[/b]

  11. Deepak Says:

    IanF, no worries.

Leave a Reply