这是使用LINQ( to SQL )连接3(或更多)表的正确方法吗?特别是select
部分。我是否在正确的轨道上返回跨越表格的单个记录(行)?
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,
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();
}
数据库结构如下 - >
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
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
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
对不起它结束了这么久。如果你还在我身边,感谢你和奖励积分,如果有人可以在评论中指出我如何在仍然提出问题的同时缩短这一点。我将不胜感激以供将来参考。
答案 0 :(得分:3)
假设您的关系在dbml中定义良好,生成的类应该允许您像这样查询:
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
};
基本上,所有表关系都存在于对象结构中。您使用对象关系执行查询,并且所有SQL连接都由对象属性上的属性推断(在代码生成步骤中设置)。
一个小技巧,我总是将表放在 from 子句中关系的“多”方面,这样我就可以myObject.Parent.Property
。如果我有一个多对多表,那么这个是在from子句中。这样做,我只需要使用显式连接: