Multi Join Linq声明

时间:2009-11-23 17:25:20

标签: c# linq linq-to-sql join

这是使用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();
    }

数据库结构如下 - >

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

对不起它结束了这么久。如果你还在我身边,感谢你和奖励积分,如果有人可以在评论中指出我如何在仍然提出问题的同时缩短这一点。我将不胜感激以供将来参考。

1 个答案:

答案 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子句中。这样做,我只需要使用显式连接:

  • 左连接
  • 需要以多对一,一对多,多对多的方式获取数据的查询。在这种情况下,更改关系方向的表(从多对一到一对多)是join子句中的表。