如何将以下SQL查询转换为LINO

时间:2010-09-21 10:04:03

标签: linq

SELECT     t_PersonalInformation.personalInformation_Name,             t_PersonalInformation.personalInformation_PresentAddress, 
           t_PersonalInformation.personalInformation_PermanentAddress, t_PersonalInformation.personalInformation_Phone, 
           t_PersonalInformation.personalInformation_Email,

           t_Applicant.applicant_TotalExperience,

           t_Experience.experience_CompanyName, CAST( t_Experience.experience_EndingYear AS INT) - CAST( t_Experience.experience_JoiningYear AS INT) AS yearOfExperience ,
           t_Experience.experience_Responsibilities,

           t_Training.training_TitleDetails,   t_Training.training_Institute,
           t_Training.training_Year,           t_Training.training_Duration


FROM         t_Applicant LEFT OUTER JOIN
             t_PersonalInformation ON  t_Applicant.applicant_user_ID = t_PersonalInformation.personalInformation_applicant_ID

             LEFT OUTER JOIN   
             t_Experience          ON  t_Applicant.applicant_user_ID = t_Experience.experience_applicant_ID

             LEFT OUTER JOIN
             t_Training            ON  t_Applicant.applicant_user_ID = t_Training.training_applicant_ID



WHERE     (t_Applicant.applicant_user_ID = 'hasib789') 

我在C#中使用VS2008

1 个答案:

答案 0 :(得分:0)

我没有足够的时间来完成所有这些,但我可以让你知道它是如何完成的。

var query = from var applicant in t_Applicant
    from perInf in t_PersonalInformation.Where(per => applicant.applicant_user_ID = per.personalInformation_applicant_ID).DefaultIfEmpty()
    where applicant.applicant_user_ID == "hasib789"
    select new ClassName{
        Name = perInf!=null ? perInf.personalInformation_Name : <default value>,
        PresentAddress = perInf!=null ? perInf.personalInformation_PresentAddress: <default value>,
        ...
        etc
    }

第二个'from'语句是进行外连接的方法之一。即使没有找到perInf的匹配记录,DefaultIfEmpty也允许它生成ClassName的新记录。实际上,您需要在底部的新记录中分配值,您必须检查是否为空。

使用该模板,您应该能够构建其余的查询。