Multi Join Linq Statement
Is this the proper way to accomplish joining 3 (or more) tables with LINQ (to SQL)? Especially the select
portion. Am I on the right track to return a single record(row) of data that spans across the tables?
public static DataTable GetCurrentEmploymentQuestionnaire(Guid employmentQuestionnaireID)
{
var Questionnaire = from employmentQuestionnaire in context.tblEmploymentQuestionnaires
join prevocService in context.tblEmploymentPrevocServices on
employmentQuestionnaire.PrevocServicesID equals prevocService.EmploymentPrevocID
join jobDevelopmetService in context.tblEmploymentJobDevelopmetServices on
employmentQuestionnaire.JobDevelopmentServicesID equals
jobDevelopmetService.JobDevelopmentServicesID
where employmentQuestionnaire.EmploymentQuestionnaireID == employmentQuestionnaireID
select
new
{
EmploymentQuestionnaireID = employmentQuestionnaire.EmploymentQuestionnaireID,
PlanID = employmentQuestionnaire.PlanID,
开发者_JAVA技巧 HasCommunityJob = employmentQuestionnaire.CommunityJob,
HasPrevocServices = employmentQuestionnaire.PrevocServices,
HasJobDevelopmentServices = employmentQuestionnaire.JobDevelopmentServices,
WhoCreated = employmentQuestionnaire.InsertUser,
WhenCreated = employmentQuestionnaire.InsertDate,
WhoUpdated = employmentQuestionnaire.UpdateUser,
WhenUpdated = employmentQuestionnaire.UpdateDate,
AvgRatePay = prevocService.AvgRatePay,
AvgHoursWeek = prevocService.AvgHoursWeek,
PrevocGoal = prevocService.PrevocGoal,
SkillsTaught = prevocService.SkillsTaught,
SkillsLearned = prevocService.SkillsLearned,
AnticipatedTransitionPlan = prevocService.AnticipatedTransitionPlans,
AnticipatedEndDate = prevocService.AnticipatedEndDate,
TypeWorkDesired = jobDevelopmetService.TypeWorkDesired,
NeedEmpServices = jobDevelopmetService.NeedEmploymentServices,
IsDVRProvidingServices = jobDevelopmetService.DVRProvidingServices,
DVRCurrentReferralExists = jobDevelopmetService.DVRCurrentReferral
};
return Questionnaire.CopyLinqToDataTable();
}
Database structure is as follows -->
tblEmploymentQuestionnaire
EmploymentQuestionnaireID uniqueidentifier Unchecked
PlanID int Unchecked
CommunityJob bit Checked
PrevocServices bit Checked
PrevocServicesID uniqueidentifier Checked
InsertUser varchar(50) Checked
InsertDate datetime Checked
UpdateUser varchar(50) Checked
UpdateDate datetime Checked
JobDevelopmentServices bit Checked
JobDevelopmentServicesID uniqueidentifier Checked
tblEmploymentPrevocServices
EmploymentPrevocID uniqueidentifier Unchecked
AvgRatePay varchar(50) Checked
AvgHoursWeek varchar(50) Checked
SettingID int Checked
PrevocGoal varchar(500) Checked
SkillsTaught varchar(500) Checked
SkillsLearned varchar(500) Checked
AnticipatedTransitionPlans varchar(500) Checked
AnticipatedEndDate datetime Checked
RatioID int Checked
rowguid uniqueidentifier Unchecked
tblEmploymentJobDevelopmentService
JobDevelopmentServicesID uniqueidentifier Unchecked
TypeWorkDesired varchar(50) Checked
PreferredWorkHoursID int Checked
NeedEmploymentServices bit Checked
DVRProvidingServices bit Checked
DVRCurrentReferral bit Checked
CMOProvidingServices bit Checked
CMONotProvidingReason varchar(500) Checked
PaidCoachingHoursID int Checked
PlanSegregatedToIntegrated varchar(500) Checked
RoleResponseJobDeveloper varchar(500) Checked
RoleResponseMember varchar(500) Checked
RoleResponseWWCTeam varchar(500) Checked
PlanDVRToWWCFund varchar(500) Checked
DVRCurrentReferralStatusID int Checked
Sorry it ended up being so long. If you are still with me, thank you and bonus appreciation points if someone can point out in the comments how I could have shortened this while still asking my question. I would appreciate it for future reference.
Assuming your relationships are well defined in the dbml, the generated classes should allow you to query like this:
var Questionnaire = from employmentQuestionnaire in context.tblEmploymentQuestionnaires
where employmentQuestionnaire.EmploymentQuestionnaireID == employmentQuestionnaireID
select new
{
EmploymentQuestionnaireID = employmentQuestionnaire.EmploymentQuestionnaireID,
PlanID = employmentQuestionnaire.PlanID,
HasCommunityJob = employmentQuestionnaire.CommunityJob,
HasPrevocServices = employmentQuestionnaire.PrevocServices,
HasJobDevelopmentServices = employmentQuestionnaire.JobDevelopmentServices,
WhoCreated = employmentQuestionnaire.InsertUser,
WhenCreated = employmentQuestionnaire.InsertDate,
WhoUpdated = employmentQuestionnaire.UpdateUser,
WhenUpdated = employmentQuestionnaire.UpdateDate,
AvgRatePay = employmentQuestionnaire.PrevocService.AvgRatePay,
AvgHoursWeek = employmentQuestionnaire.PrevocService.AvgHoursWeek,
PrevocGoal = employmentQuestionnaire.PrevocService.PrevocGoal,
SkillsTaught = employmentQuestionnaire.PrevocService.SkillsTaught,
SkillsLearned = employmentQuestionnaire.PrevocService.SkillsLearned,
AnticipatedTransitionPlan = employmentQuestionnaire.PrevocService.AnticipatedTransitionPlans,
AnticipatedEndDate = employmentQuestionnaire.PrevocService.AnticipatedEndDate,
TypeWorkDesired = employmentQuestionnaire.JobDevelopmentService.TypeWorkDesired,
NeedEmpServices = employmentQuestionnaire.JobDevelopmentService.NeedEmploymentServices,
IsDVRProvidingServices = employmentQuestionnaire.JobDevelopmentService.DVRProvidingServices,
DVRCurrentReferralExists = employmentQuestionnaire.JobDevelopmentService.DVRCurrentReferral
};
Basically, all the table relationships exist in your object structure. You do your query using your object relations and all the SQL joins are inferred by the attributes on your objects properties (set-up in the code generation step).
A little trick, I always place the table on the "many" side of the relationship in the from clause so I can do myObject.Parent.Property
. If I have a many-to-many table it is this one that is in the from clause. Doing this, I only need to use explicit joins for:
- Left joins
- Queries that need to fetch data in a many-to-one, to-one, ... to-many. In that case the table that changes the relationship direction (from many-to-one to one-to-many) is the one in the join clause.
精彩评论