Joining multiple tables using Entity Framework

c# entity-framework linq

Question

i am trying to join 3 tables using EF but it throws an error saying

consider swaping conditions on either side of equals

can some one pls help

 var billdata = from billtotal in context.billTotals
                                   join billcard in context.billClubcards
                                       on billtotal.OrderID equals billcard.OrderID

                                   join billtender in context.billTenders
                                       on billtender.OrderID equals billtotal.OrderID


                                   select billtotal;
1
4
10/30/2012 3:19:00 PM

Accepted Answer

The compiler error is quite correct:-

The name 'billtender' is not in scope on the left side of 'equals'. Consider swapping the expressions on either side of 'equals'.

The table you're joining from needs to be on the left, the one you're joining onto needs to be on the right. Hence:-

var billData =
    from billtotal in context.billTotals
    join billcard in context.billClubcards
        on billtotal.OrderId equals billcard.OrderId
    join billtender in context.billTenders
        on billtotal.OrderId equals billtender.OrderId
    select billtotal;

If you're wondering why, it's because the query syntax is just syntactic sugar for the underlying extension method:-

context.billTotals
  .Join(
    context.billClubcards,
    billtotal => billtotal.OrderId,
    billclubcard => billclubcard.OrderId,
    (billtotal, billclubcard) => billtotal)
  .Join(
    context.billTenders,
    billtotal => billtotal.OrderId,
    billtender => billtender.OrderId,
    (billtotal, billtender) => billtotal);

Your original implementation would expand to:-

context.billTotals
  .Join(
    context.billClubcards,
    billtotal => billtotal.OrderId,
    billclubcard => billclubcard.OrderId,
    (billtotal, billclubcard) => billtotal)
  .Join(
    context.billTenders,
    billtotal => billtender.OrderId, // billtender isn't in scope!
    billtender => billtotal.OrderId, // billtotal isn't in scope!
    (billtotal, billtender) => billtotal);
13
10/30/2012 3:26:49 PM

Popular Answer

The property of the table you are joining needs to be on the right side of equals

switch

from: on billtender.OrderID equals billtotal.OrderID

to: on billtotal.OrderID equals billtender.OrderID



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow