3

好的,我正在尝试将以下 SQL 查询复制到 Linq 表达式中:

SELECT
    I.EmployeeNumber,
    E.TITLE,
    E.FNAM, 
    E.LNAM
FROM
    Incidents I INNER JOIN Employees E ON I.IncidentEmployee = E.EmployeeNumber
GROUP BY
    I.EmployeeNumber,
    E.TITLE,
    E.FNAM, 
    E.LNAM

很简单(或者至少我认为):

var query = (from e in contextDB.Employees
              join i in contextDB.Incidents on i.IncidentEmployee = e.EmployeeNumber
              group e by new { i.IncidentEmployee, e.TITLE, e.FNAM, e.LNAM } into allIncEmps
              select new
                          {
                              IncEmpNum = allIncEmps.Key.IncidentEmployee
                              TITLE = allIncEmps.Key.TITLE,
                              USERFNAM = allIncEmps.Key.FNAM,
                              USERLNAM = allIncEmps.Key.LNAM
                          });

但是我没有得到我期望的结果,所以我启动了 SQL Profiler 以查看通过管道发送到 SQL Server 的内容,这就是我所看到的:

SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    COUNT(1) AS [A1]
    FROM ( SELECT DISTINCT 
        [Extent2].[IncidentEmployee] AS [IncidentEmployee], 
        [Extent1].[TITLE] AS [TITLE], 
        [Extent1].[FNAM] AS [FNAM], 
        [Extent1].[LNAM] AS [LNAM]
        FROM  [dbo].[Employees] AS [Extent1]
        INNER JOIN [dbo].[INCIDENTS] AS [Extent2] ON ([Extent1].[EmployeeNumber] = [Extent2].[IncidentEmployee]) OR (([Extent1].[EmployeeNumber] IS NULL) AND ([Extent2].[IncidentEmployee] IS NULL))
    )  AS [Distinct1]
)  AS [GroupBy1]

从发送到 SQL Server 的 SQL 字符串中可以看出,我期望返回的字段都没有包含在 Select 子句中。我究竟做错了什么?

更新

这是漫长的一天,我再次重新运行代码,现在这是正在发送到管道中的 SQL:

SELECT 
[Distinct1].[IncidentEmployee] AS [IncidentEmployee], 
[Distinct1].[TITLE] AS [TITLE], 
[Distinct1].[FNAM] AS [FNAM], 
[Distinct1].[LNAM] AS [LNAM]
FROM ( SELECT DISTINCT 
    [Extent1].[OFFNUM] AS [OFFNUM], 
    [Extent1].[TITLE] AS [TITLE], 
    [Extent1].[FNAM] AS [FNAM], 
    [Extent1].[LNAM] AS [LNAM]
    FROM  [dbo].[Employees] AS [Extent1]
    INNER JOIN [dbo].[INCIDENTS] AS [Extent2] ON ([Extent1].[EmployeeNumber] = [Extent2].[IncidentEmployee]) OR (([Extent1].[EmployeeNumber] IS NULL) AND ([Extent2].[IncidentEmployee] IS NULL))
)  AS [Distinct1]

但是当我尝试遍历记录集时仍然没有看到结果

foreach (var emps in query)
{

}
4

1 回答 1

1

不知道为什么查询不返回它应该返回的内容,但我想到,因为您只查询组键而不是任何分组结果,所以除了 a 之外什么都没有Distinct()

var query =
(from e in contextDB.Employees
 join i in contextDB.Incidents on i.IncidentEmployee equals e.EmployeeNumber
 select new
 {
     IncEmpNum = i.IncidentEmployee
     TITLE = e.TITLE,
     USERFNAM = e.FNAM,
     USERLNAM = e.LNAM
 }).Distinct();

但是 EF 足够聪明,也能看到这一点,并且也创建了一个 DISTINCT 查询。

您没有指定您期望的结果以及实际结果以何种方式不同,但我真的看不出分组如何产生与 a 不同的结果Distinct

But how did your code compile? As xeondev noticed: there should be an equals in stead of an = in a join statement. My compiler (:D) does not swallow it otherwise. The generated SQL join is strange too: it also matches records where both joined values are NULL. This makes me suspect that at least one of your keys (i.IncidentEmployee or e.EmployeeNumber) is nullable and you should either use i.IncidentEmployee.Value or e.EmployeeNumber.Value or both.

于 2012-09-29T17:48:53.650 回答