Join查询返回空结果

时间:2012-06-08 12:09:34

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

我试图将以下代码加入几张桌子

  var result = (from n in db.tbl_NAWs
                join s in db.Status on n.Status equals s.StatusID
                join a in db.tbl_Afdelings on n.Afdeling equals a.ID_Afdeling
                join l in db.Locaties on n.Locatie equals l.LocatieID
                select new NAW_Dto()
                {
                    ClassNR = n.ClassNr,
                    Status = s.Beschrijving,
                    Client = n.Aanspreekvorm,
                    Locatie = l.Naam,
                    Afdeling = a.Afdeling,
                    KamerNr = n.Kamernummer
                }).ToList();

但它一直在空洞,任何想法?

3 个答案:

答案 0 :(得分:0)

运行Sql Profiler,检查正在执行的实际SQL查询是什么,并查看您的数据。

如果没有看到您的数据,就无法告诉您为什么没有空结果集。您的外键是否按照您认为的方式设置?

尝试执行此查询并删除一些联接 - 您将能够以这种方式找到违规加入。

答案 1 :(得分:0)

尝试这样的事情......

  var result = (from n in db.tbl_NAWs
                join s in db.Status on n.Status equals s.StatusID
                join a in db.tbl_Afdelings on n.Afdeling equals a.ID_Afdeling
                join l in db.Locaties on n.Locatie equals l.LocatieID
                select new 
                {
                    ClassNR = n.ClassNr,
                    Status = s.Beschrijving,
                    Client = n.Aanspreekvorm,
                    Locatie = l.Naam,
                    Afdeling = a.Afdeling,
                    KamerNr = n.Kamernummer
                }).ToList();

答案 2 :(得分:0)

将其分解为SQL并在SSMS中运行它 一次删除一个注释,看看哪个不匹配

select n.ClassNr
-- , s.Beschrijving
-- , a.Afdeling
-- , l.Naam
from tbl_NAWs as n
-- join Status as s on n.Status equals s.StatusID
-- join tbl_Afdelings as a on n.Afdeling equals a.ID_Afdeling
-- join Locaties as l on n.Locatie equals l.LocatieID