2010-12-15 196 views
2

如何使用C#將這個SQL寫入Linq to SQL。我無法將狀態表加入ConsumerApplications和RepairOrderEstimates以正常工作。謝謝。Linq to SQL加入兩個不同表的兩次相同表

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

我認爲你可以做的東西像這樣

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, 
}