1

我很茫然,所以我来这里看看大家对此有何看法。

背景:使用带有 EF 5 的 jquery 自动完成的文本框

问题:实体正在执行内连接,但语法显示正在执行左连接。(两台 PC 都运行 Win 7 和最新框架)

异常:它在我的 PC 上以正确的方式工作(左外连接)。但在我的好友电脑上,它显示了一个内部连接。这两个文件都是二进制相等的。(实际上是从 git 中拉出来的)

这是代码:

public JsonResult AutoCompleteName(string term)
        {
            using (var db = new PersonnelContext())
            {
                return this.Json((from r in db.Personnel
                                  join per in db.PersonnelEmployee on r.Id equals per.Personnel_Id
                                  join dep in db.RefDepartment on per.Department equals dep.Department into rfdp
                                  from g in rfdp.DefaultIfEmpty()
                                  where r.First_Name.ToLower().Contains(term.ToLower()) | r.Last_Name.ToLower().Contains(term.ToLower()) | (r.First_Name.ToLower() + " " + r.Last_Name.ToLower()).Contains(term.ToLower())
                                  select new { firstname = r.First_Name, lastname = r.Last_Name, department = g.DeptDesc ?? "None", per.Personnel_Id, per.Pernr }).OrderBy(a => a.firstname).ToArray(), JsonRequestBehavior.AllowGet);
        }
    }

这是来自 PC 的智能跟踪,它工作正常:(注意,由于机密性,我取出了一些)

FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[First_Name] AS [First_Name], 
    [Extent1].[Last_Name] AS [Last_Name], 
    [Extent2].[Personnel_Id] AS [Personnel_Id], 
    [Extent2].[Pernr] AS [Pernr], 
    CASE WHEN ([Extent3].[Dept_Desc] IS NULL) THEN N'None' ELSE [Extent3].[Dept_Desc] END AS [C1]
    FROM   [Core].[Personnel] AS [Extent1]
    INNER JOIN [Core].[Personnel_Employee] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Personnel_Id]
    LEFT OUTER JOIN [Core].[Ref_Department] AS [Extent3] ON [Extent2].[Department] = [Extent3].[Department]
    WHERE (( CAST(CHARINDEX(LOWER(@p__linq__0), LOWER([Extent1].[First_Name])) AS int)) > 0) OR (( CAST(CHARINDEX(LOWER(@p__linq__1), LOWER([Extent1].[Last_Name])) AS int)) > 0) OR (( CAST(CHARINDEX(LOWER(@p__linq__2), LOWER([Extent1].[First_Name]) + N' ' + LOWER([Extent1].[Last_Name])) AS int)) > 0)
)  AS [Project1]
ORDER BY [Project1].[First_Name] ASC"

如您所见,正如语法所暗示的那样,它执行左外连接。

这是来自 PC 2 的 Intellitrace:(完全相同的代码!)

FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[First_Name] AS [First_Name], 
    [Extent1].[Last_Name] AS [Last_Name], 
    [Extent2].[Personnel_Id] AS [Personnel_Id], 
    [Extent2].[Pernr] AS [Pernr], 
    CASE WHEN ([Extent3].[Dept_Desc] IS NULL) THEN N'None' ELSE [Extent3].[Dept_Desc] END AS [C1]
    FROM   [Core].[Personnel] AS [Extent1]
    INNER JOIN [Core].[Personnel_Employee] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Personnel_Id]
    INNER JOIN [Core].[Ref_Department] AS [Extent3] ON [Extent2].[Department] = [Extent3].[Department]
    WHERE (( CAST(CHARINDEX(LOWER(@p__linq__0), LOWER([Extent1].[First_Name])) AS int)) > 0) OR (( CAST(CHARINDEX(LOWER(@p__linq__1), LOWER([Extent1].[Last_Name])) AS int)) > 0) OR (( CAST(CHARINDEX(LOWER(@p__linq__2), LOWER([Extent1].[First_Name]) + N' ' + LOWER([Extent1].[Last_Name])) AS int)) > 0)
)  AS [Project1]
ORDER BY [Project1].[First_Name] ASC"

这是另一个奇怪的异常情况:当部署到 QA 时,它在两台 PC 上都能正常工作!!这里没有一致性让我们知道发生了什么。

这里有什么想法吗?我不知道发生了什么。我们已经完全清除了我们的工作版本,并再次从 git 中删除。同样的事情也会发生。如果没有人有任何想法,我们可能需要使用 proc。但我至少想知道发生了什么。

** 编辑 ** 答:我真的不知道为什么会这样,但我得到了它的工作。这是现在的查询:

return this.Json((from r in db.Personnel
                                  join per in db.PersonnelEmployee on r.Id equals per.Personnel_Id
                                  where r.First_Name.ToLower().Contains(term.ToLower()) | r.Last_Name.ToLower().Contains(term.ToLower()) | (r.First_Name.ToLower() + " " + r.Last_Name.ToLower()).Contains(term.ToLower())
                                  join dep in db.RefDepartment
                                  .Where(x => x.DeptDesc != null || x.DeptDesc == null) on per.Department equals dep.Department into rfdp
                                  from g in rfdp.DefaultIfEmpty()
                                  select new { 
                                      firstname = r.First_Name,
                                      lastname = r.Last_Name, 
                                      department = (g == null) ? "None" : g.DeptDesc,
                                      per.Personnel_Id, 
                                      per.Pernr }).ToArray(), JsonRequestBehavior.AllowGet);

我认为这个有效,因为它明确地寻找 DeptDesc 为空而不是空......我很高兴它的工作。感谢所有看到这个的人。

4

2 回答 2

1

我能够通过以下修改后的查询来解决这个问题:

return this.Json((from r in db.Personnel
                                  join per in db.PersonnelEmployee on r.Id equals per.Personnel_Id
                                  where r.First_Name.ToLower().Contains(term.ToLower()) | r.Last_Name.ToLower().Contains(term.ToLower()) | (r.First_Name.ToLower() + " " + r.Last_Name.ToLower()).Contains(term.ToLower())
                                  join dep in db.RefDepartment
                                  .Where(x => x.DeptDesc != null || x.DeptDesc == null) on per.Department equals dep.Department into rfdp
                                  from g in rfdp.DefaultIfEmpty()
                                  select new { 
                                      firstname = r.First_Name,
                                      lastname = r.Last_Name, 
                                      department = (g == null) ? "None" : g.DeptDesc,
                                      per.Personnel_Id, 
                                      per.Pernr }).ToArray(), JsonRequestBehavior.AllowGet);

正如我在编辑中所说的那样:我不是 100% 确定为什么它会起作用而不是第一个,但我很高兴它起作用了!

于 2013-06-27T15:17:00.463 回答
0

This sounds suspiciously like an already 'fixed' issue with EF... have a look at this duplicated join issue

They note that removing the orderby removes the duplicate... worth a test eh

于 2013-06-27T14:32:33.520 回答