5

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;

2 Answers 2

14

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.