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 8246005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:22:01+00:00 2026-06-07T22:22:01+00:00

In an attempt to convert this SQL query to LINQ I have hit a

  • 0

In an attempt to convert this SQL query to LINQ I have hit a problem with group by.
How can I group by multiple columns that originate from multiple tables?
The linq query below simply won’t compile so I’m in need of the correct syntax

Original SQL Query

select LTRIM(RTRIM(e.Shortname)) 
Name, LTRIM(RTRIM(j.JobName)) 
JobName, j.JobCode, 
SUM(Hours) Hours, 
MAX(ce.LatestTimesheetEntry) LastTimeSubmitted 
from TWCTL.TW.Postings p
join TWCTL.TW.Employees e on e.EmployeeId = p.EmployeeId
join TWCTL.TW.Jobs j on j.JobCode = p.JobCode
join CentralTimeEmployees.dbo.Employees ce on ce.CtEmployeeId = e.EmployeeId
where (e.CostCentreId = 1 or e.CostCentreId = 3) 
and (p.TransactionDate >= '2012-01-01' and p.TransactionDate <= '2012-07-01') 
and j.JobCode <> 'CTCIT00001' 
and ce.DatabaseCode = 'CTL' 
and (ce.CostCentreId = 1 or ce.CostCentreId = 3)
group by j.JobName, j.JobCode, Shortname
order by e.Shortname, j.JobName

Linq Query attempt (not working)

var model = 
        from p in context.Postings
        join e in context.Employees on p.EmployeeId equals e.EmployeeId
        join j in context.Jobs on p.JobCode equals j.JobCode
        join ce in context.CentralTimeEmployees on e.EmployeeId equals ce.CtEmployeeId
        where (e.CostCentreId == 5 || e.CostCentreId == 3)
                && (p.TransactionDate >= fromDate
                    && p.TransactionDate <= toDate)
                && j.JobCode != "CTCIT00001"
                && ce.DatabaseCode == "CTL"
                && (ce.CostCentreId == 1 || ce.CostCentreId == 3)
        group j by new {j.JobName, j.JobCode} into g1
        group e by e.Shortname into g2    <- doesnt work??        
        select
            new ProjectHoursViewModel
                {Posting = p, Job = g1.Key.JobName, Employee = e, CentralTimeEmployee = ce};
  • 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-07T22:22:03+00:00Added an answer on June 7, 2026 at 10:22 pm

    For starters, your query is not equivalent. Minor things really like different values being checked but a major one is that you don’t group by the same keys. The SQL grouped by JobCode which you left out in your query. And you attempted to group too much instead of ordering it. The values you are aggregating should be what you are grouping.

    As for why you’re getting the syntax error, after you do a continuation (using into in a group by or select clause), all the variables in the previous scope are lost and only the continuation variable remains (g1 in your case). You tried to reference e which is now out of scope giving you the problem.

    The query should be more like this I think:

    var fromDate = new DateTime(2012, 1, 1);
    var toDate = new DateTime(2012, 7, 1);
    var query = 
        from p in dc.Postings
        join e in dc.Employees on p.EmployeeId equals e.EmployeeId
        join j in dc.Jobs on p.JobCode equals j.JobCode
        join ce in dc.CentralTimeEmployees on e.EmployeeId equals ce.CtEmployeeId
        where (e.CostCentreId == 1 || e.CostCentreId == 3) // the SQL tested 1 or 3
           && (p.TransactionDate >= fromDate && p.TransactionDate <= toDate)
           && j.JobCode != "CTCIT00001"
           && ce.DatabaseCode == "CTL"
           && (ce.CostCentreId == 1 || ce.CostCentreId == 3)
        group new { ce.Hours, ce.LatestTimesheetEntry }
           by new { j.JobName, j.JobCode, e.ShortName } into g
        orderby g.Key.ShortName, g.Key.JobName
        select new
        {
            Name = g.Key.ShortName.Trim(),
            JobName = g.Key.JobName.Trim(),
            JobCode = g.Key.JobCode,
            Hours = g.Sum(x => x.Hours),
            LastTimeSubmitted = g.Max(x => x.LatestTimesheetEntry),
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working sql query that I have been trying to convert to
I am trying to convert this short SQL statement into linq but I am
I'm using this code to attempt to convert a float to an int, then
I wrote this python code in an attempt to convert objects to a string
I have realized that my first question won't be answerable, since php can't deal
I am trying to convert the following line of SQL to LINQ code :
i need to convert this one from SQL Server into MySQL IF IsNull(@SearchText, '')
I have a dynamic sql query in SQL Server. So I build it, set
In order to answer this question properly I thought that I would: Convert an
How do I convert a string into a boolean in Python? This attempt returns

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.