Linq to SQL为两个不同的表连接到同一个表两次

时间:2010-12-15 02:22:35

标签: c# linq-to-sql join

如何使用C#在Linq to SQL中编写此SQL。我无法将状态表连接到ConsumerApplications和RepairOrderEstimates以正常工作。感谢。

select ca.ConsumerAppID,
       ca.LastName,
       statConsumerApp.StatusName,
       statRepairOrderEstimates.StatusName 
  from ConsumerApplications ca
  join RepairOrderEstimates
    on ca.RepairOrderEstimateID = RepairOrderEstimates.RepairOrderEstimateID
  join Statuses statConsumerApp
    on ca.StatusID = statConsumerApp.StatusID
  join Statuses statRepairOrderEstimates
    on RepairOrderEstimates.StatusID = statRepairOrderEstimates.StatusID

1 个答案:

答案 0 :(得分:2)

我认为你可以用

之类的东西来做到这一点
from ca in ConsumerApplications
join est in RepairOrderEstimates on ca.RepairOrderEstimateID == est.RepairOrderEstimateID
join statConsumerApp in Statuses on ca.StatusID == statConsumerApp.StatusID
join statEstimate in Statuses on est.StatusID == statEstimate.StatusID
select new {
  ConsumerAppID = ca.ConsumerAppID,
  LastName = ca.LastName,
  AppStatus = statConsumerApp.StatusName,
  EstimateStatus = statEstimate.StatusName,
}