简体   繁体   中英

Joining multiple tables using Entity Framework

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;

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM