简体   繁体   English

使用Entity Framework连接多个表

[英]Joining multiple tables using Entity Framework

i am trying to join 3 tables using EF but it throws an error saying 我正在尝试使用EF加入3个表,但它会抛出一个错误说

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 您加入的表的属性需要在equals的右侧

switch 开关

from: on billtender.OrderID equals billtotal.OrderID from: on billtender.OrderID equals billtotal.OrderID

to: on billtotal.OrderID equals billtender.OrderID to: on billtotal.OrderID equals billtender.OrderID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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