C# 使用实体框架连接多个表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13141862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:44:36  来源:igfitidea点击:

Joining multiple tables using Entity Framework

c#linqentity-framework

提问by

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;

采纳答案by Iain Galloway

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

回答by Aducci

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

从: on billtender.OrderID equals billtotal.OrderID

to: on billtotal.OrderID equals billtender.OrderID

到: on billtotal.OrderID equals billtender.OrderID