none
Problem with Linq joins RRS feed

  • Question

  •  

    Hi

     

    I have a problem with a linq join. Can anyone see why I should have the od and o on different sides of the equal?

     

    var result = from o in dc.Portal_Offices

    join od in dc.Office_Departments on od.Office equals o.Office

     

    Cheers

    Friday, April 18, 2008 3:31 PM

Answers

  • The answer is "just because Linq looks a lot like SQL, don't mean it really is SQL".

     

    Linq queries are translated into standard function notation, so that

     

    var result = from o in dc.Orders

    join od in dc.OrderDetails on o.OrderID equals od.OrderID

    select new { o.ShipName, od.UnitPrice }

     

    (I had to switch to Northwind database, since I don't have yours)

     

    gets translated into :

    dc.Orders
       .Join (
          dc.OrderDetails,
          o => o.OrderID,
          od => od.OrderID,
          (o, od) =>
             new 
             {
                ShipName = o.ShipName,
                UnitPrice = od.UnitPrice
             }
       )

    So you can see how the order of the operands becomes important.

     

    (note, this translation was done using the free utility LinqPad  www.linqpad.net )

     

    Saturday, April 19, 2008 12:05 PM

All replies

  • I don't understand your question, can you clarify?

     

    Friday, April 18, 2008 4:12 PM
  •  

    It complains that I have to swap

     

    od.office equals o.office

     

    to

     

    o.office equals od.office

     

    Is the 2nd the correct way of doing it because I've seen exampls on sites of the 1st but that doesn't work for me.

    Friday, April 18, 2008 4:15 PM
  • I do not know from where you read the data. But I would like to see this project or the data if possible. I can not imagine that this is important. I habe done something like that with Linq to Objects and it works fine. I hava also done some really big project with LINQ and it is really useful.

     

    Friday, April 18, 2008 4:53 PM
  •  levyuk1 wrote:

     

    It complains that I have to swap

     

    od.office equals o.office

     

    to

     

    o.office equals od.office

     

    Is the 2nd the correct way of doing it because I've seen exampls on sites of the 1st but that doesn't work for me.

     

    It is the same thing, but the second is a good practice.

    Saturday, April 19, 2008 8:47 AM
  • The answer is "just because Linq looks a lot like SQL, don't mean it really is SQL".

     

    Linq queries are translated into standard function notation, so that

     

    var result = from o in dc.Orders

    join od in dc.OrderDetails on o.OrderID equals od.OrderID

    select new { o.ShipName, od.UnitPrice }

     

    (I had to switch to Northwind database, since I don't have yours)

     

    gets translated into :

    dc.Orders
       .Join (
          dc.OrderDetails,
          o => o.OrderID,
          od => od.OrderID,
          (o, od) =>
             new 
             {
                ShipName = o.ShipName,
                UnitPrice = od.UnitPrice
             }
       )

    So you can see how the order of the operands becomes important.

     

    (note, this translation was done using the free utility LinqPad  www.linqpad.net )

     

    Saturday, April 19, 2008 12:05 PM
  •  

    Ok guys I've figured it out in the end. Used sql instead as the linq was getting a bit complex.

     

    Thanks for your help.

    Monday, April 21, 2008 9:51 AM