如何使用Entity Framework连接具有不同数据类型的表

时间:2013-09-25 01:54:42

标签: entity-framework linq-to-entities

在使用Entity Framework的情况下,我想连接具有相同值的两个字段的表,但是字段用不同的数据类型标识。例如,存在数据类型为“Guid”的字段,但在另一个表中,关联字段为“字符串”类型。我很难实现这个目标。有人可以给我一些想法。这是我的代码,emps.EmpGuid是'Guid'数据类型,flowCode.Object是一个字符串:

DbSet<tb_Emp_Manager_Zbdl> dbEmp = zbdlcontext.Emp_Manager;
DbSet<Tb_Corp_CompanyInfo_Zbdl> dbCompany = zbdlcontext.Corp_CompanyInfos;
DbSet<Tb_FlowCode_Zbdl> dbFlowCode = zbdlcontext.FlowCodes;
DbSet<Tb_Emp_Post_Zbdl> dbEmpPost = zbdlcontext.Emp_Post;
var query = from emps in dbEmp
            join companies in dbCompany on emps.CorpGuid equals companies.CorpUserGuid
            join flowCode in dbFlowCode on new { EmpGuid = emps.EmpGuid.ToString(), AreaCode = areaCode } equals new { EmpGuid = flowCode.ObjectId, AreaCode = flowCode.AreaCode } into jFlowCodes
            from flowCodes in jFlowCodes.DefaultIfEmpty()
            join post in dbEmpPost on emps.EmpGuid equals post.EmpGuid into jPosts
            from posts in jPosts.DefaultIfEmpty()
            select new tb_Emp_Manager()
                       {
                            EmpGuid = emps.EmpGuid,
                            AreaCode = emps.AreaCode,
                            FlowCode = flowCodes.FlowCode,
                            corpName = companies.CorpName
                       };

0 个答案:

没有答案