5

我正在尝试使用 EF 加入 3 个表,但它会抛出一个错误说

consider swaping conditions on either side of equals

有人可以帮忙吗

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

2 回答 2

14

编译器错误非常正确:-

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

您要加入的表需要在左侧,您要加入的表需要在右侧。因此:-

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;

如果您想知道为什么,那是因为查询语法只是底层扩展方法的语法糖:-

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

您的原始实现将扩展为:-

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);
于 2012-10-30T15:12:20.797 回答
0

您要加入的表的属性需要在 equals 的右侧

转变

从: on billtender.OrderID equals billtotal.OrderID

到: on billtotal.OrderID equals billtender.OrderID

于 2012-10-30T15:08:08.127 回答