Lakeshore
Lakeshore

Reputation: 323

Linq to SQL Joining to the Same Table Twice for Two Different Tables

How do I write this SQL in Linq to SQL using C#. I cannot get the join to the status table to both ConsumerApplications and RepairOrderEstimates to work properly. Thanks.

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

Upvotes: 4

Views: 10000

Answers (1)

Jeffrey Hantin
Jeffrey Hantin

Reputation: 36534

I think you can do this with something like

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

Upvotes: 4

Related Questions