none
JOINs in LINQ RRS feed

  • Question

  • i have two tables :

    table 1

    m1  y1 count1

    Jan  2011 115

    Mar 2011 250

    table 2

    m2 y2  count2

    Jan 2011 0

    Feb 2011 0

    Mar 2011 0

    i want result in the form of table as :

    Desired Table -

    m  count

    Jan  2011 115

    Feb 2011 0

    Mar 2011 250

    can anybody tell me the Linq code for "Desired Table" ???

    How to join the tables to get the above result ???


    Wednesday, March 21, 2012 1:44 PM

Answers

All replies

  • Hi LINK_me_with_LINQ;

    A couple of questions.

    1. Are the two tables SQL server tables?

    2. If so what is the primary ky fields?

    3. A table join would return selected columns from both table depending on a column value from both tables, but it looks like you are replacing values?

    4. Can there be multiple m and y in each table having the same values? If so what is the rule for handling this case.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, March 21, 2012 6:11 PM
  • try this:

    var x = form n in table1
            join m in table2
          on n.count1 equals m.coun2 into aux
          from y in aux
           select new {m = y.m1, y = y.y1, count= y.count1   };

    for more info:

    http://msdn.microsoft.com/us-en/library/bb311040.aspx

    Wednesday, March 21, 2012 8:10 PM
  • Actually both of them are in arrays , but i m treating them as tables to get the desired result.
    Thursday, March 22, 2012 5:08 AM
  • Please Help 

    thanks jose but not working fine :(:(:(

    Thursday, March 22, 2012 5:41 AM
  • jose i mean feb 2011 0 row is not coming 
    Thursday, March 22, 2012 8:45 AM
  • The problem must be here: (in bold)

    var x = form n in table1
            join m in table2
          on n.count1 equals m.coun2 into aux
          from y in aux
           select new {m = y.m1, y = y.y1, count= y.count1   };

    that's the criterion by which select in the two tables. 

    I put this to illustrate the point, but to get the desired result you must put the condition in which you want to make the join.


    Thursday, March 22, 2012 5:51 PM
  • Thanks Jose
    Monday, March 26, 2012 5:38 AM