none
join on multiple columns RRS feed

  • Question

  •  

    Hi,

    I was asked the next question on the job interview:

     

    Given the following tables, create a LINQ query that does a join on multiple columns:


    MEMBERS
           MemberID
           FirstName
           LastName
           TypeCode               -- maps to LookUpKey with a category of TYPE

    LOOKUPS
           LookupID
           Category
           LookUpKey
           Description

    My decision was:

    var query = from a in db.MEMBERS
            join b in db.LOOKUPS
            on a.TypeCode equals b.LookUpKey
    select a; 

    My questions are: is this decision correct? and why it was mentioned category of TYPE?

    Thanks

    Saturday, July 9, 2011 6:35 PM

Answers

  • Hi Den,

    I think the question was to join on MULTIPLE columns which is trickier in Linq.

    For this you need to use anonymous types like here:

    var result = from x in table1
      join y in table2 on new { x.field1, x.field2 } equals new { y.field1, y.field2 }
    
    
    


    I think the task was to join the two tables with two columns, but it is not clear for me which ones should be used for this...

     


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    Monday, July 11, 2011 8:45 AM

All replies

  • Hi Den,

    I think the question was to join on MULTIPLE columns which is trickier in Linq.

    For this you need to use anonymous types like here:

    var result = from x in table1
      join y in table2 on new { x.field1, x.field2 } equals new { y.field1, y.field2 }
    
    
    


    I think the task was to join the two tables with two columns, but it is not clear for me which ones should be used for this...

     


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    Monday, July 11, 2011 8:45 AM
  • How's it going? Please feel free to let us know if you need any further support.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 12, 2011 7:33 AM