Mark Kram
Mark Kram

Reputation: 5842

Linq Join with a Group By

Ok, I am trying to replicate the following SQL query into a Linq expression:

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

Simple enough (or at least I thought):

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
                          });

But I am not getting back the results I exprected, so I fire up SQL Profiler to see what is being sent down the pipe to SQL Server and this is what I see:

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]

As you can see from the SQL string that was sent toSQL Server none of the fields that I was expecting to be return are being included in the Select clause. What am I doing wrong?

UPDATE

It has been a very long day, I re-ran the code again and now this is the SQL that is being sent down the pipe:

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]

But I am still not seeing results when I try to loop through the record set

foreach (var emps in query)
{

}

Upvotes: 3

Views: 215

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109251

Not sure why the query does not return what it should return, but it occurred to me that since you only query the group key and not any grouped results you've got nothing but 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();

But EF was smart enough to see this as well and created a DISTINCT query too.

You don't specify which result you expected and in what way the actual result was different, but I really can't see how the grouping can produce a different result than 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.

Upvotes: 1

Related Questions