Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8676751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:16:35+00:00 2026-06-12T20:16:35+00:00

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

  • 0

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)
{

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:16:37+00:00Added an answer on June 12, 2026 at 8:16 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to replicate the following sql in Nhibernate ICriteria SELECT DISTINCT AP.ID FROM
Trying to replicate the following query using Zend_Db_Select . Any pointers? SELECT compounds.id as
I am trying to replicate the following formula from Excel in to a C#
I am trying to replicate the IF function from MySQL into PostgreSQL. The syntax
I am trying to replicate a database from SQL server 2000 to 2005 they
I am trying to replicate the following code using HTML Helper in CakePHP 2.1.
I'm trying to replicate something similar to the following map, where the polygonal area
I am trying to replicate a table produced from a sybase database, I dont
I have been trying to replicate the buffer overflow example3 from this article aleph
I'm trying to replicate the following Ninject syntax in Unity, but not having any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.