使用内部联接帮助将SQL转换为LINQ. [英] help to convert SQL to LINQ with inner join.

查看:72
本文介绍了使用内部联接帮助将SQL转换为LINQ.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..请帮助我将其转换为linq

hi.. please help me convert this to linq

SELECT     dbo.tblPersonnel.empCode, dbo.tblPersonnel.areaCode, dbo.tblPersonnel.branhCode, dbo.tblPersonnel.regionCode
FROM         dbo.tblPersonnel INNER JOIN
                      dbo.sg_branch ON dbo.tblPersonnel.regionCode = dbo.sg_branch.Region AND dbo.tblPersonnel.areaCode = dbo.sg_branch.Area AND 
                      dbo.tblPersonnel.branhCode = dbo.sg_branch.BranchCode



我在这里:



i have here:

var Personnel = from tblpersonnel in db.tblPersonnel
                join tblbranch in db.sg_branch on tblpersonnel.branhCode equals tblbranch.BranchCode
                where (tblpersonnel.branhCode.Equals(tmpBranch))
                orderby tblpersonnel.empFname
                select tblpersonnel;



我不知道有多个联接..请增强..



i don''t know with multiple joins.. please enhance..

推荐答案

您只能比较标量或对象.因此,您需要在两边创建具有相同字段名称的内联对象,并比较它们,而不是值.请参阅此处的描述: http://www.deepakkapoor.net/linq-to- sql-join-on-multiple-conditions/ [ ^ ].请注意,名称很重要,因此,如果两个表中的字段名称不同,则必须添加自己的名称.

请参见具有相应字段名称的以下示例:
You can compare only scalars or objects. So you need to create inline objects with same field names on both sides and compare those, not the values. See the description here: http://www.deepakkapoor.net/linq-to-sql-join-on-multiple-conditions/[^]. Be aware, that names mater, thus if your field names are not the same in the two tables, you have to add your own names.

See this example with corresponding field names:
var query = from s in context.TableA
            join h in context.TableB
            on
            new { s.Field1, s.Field2 }
            equals
             new { h.Field1, h.Field2 }
            select s;


而这个具有不对应的字段名称:


And this one with non-corresponding field names:

var query = from s in context.TableA
            join h in context.TableB
            on
            new { F1 = s.Field1, F2 = s.Field2 }
            equals
             new { F1 = h.Field3, F2 = h.Field4 }
            select s;


尝试以下查询
Try the below query
var Personnel = from tblpersonnel in db.tblPersonnel
                join tblbranch in db.sg_branch on tblpersonnel.branhCode equals tblbranch.BranchCode where (tblpersonnel.branhCode.Equals(tmpBranch)) orderby tblpersonnel.empFname 
select new {tblPersonnel.empCode, tblPersonnel.areaCode, tblPersonnel.branhCode, tblPersonnel.regionCode};

Personnel.ToList();//This line will geve you the list of the result


这篇关于使用内部联接帮助将SQL转换为LINQ.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆