编译器错误非常正确:-
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);