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

  • Home
  • SEARCH
  • 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 7012201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:13:30+00:00 2026-05-27T22:13:30+00:00

I have var result = (from rev in Revisions join usr in Users on

  • 0

I have

var result =  (from rev in Revisions 
   join usr in Users on rev.UserID equals usr.ID
    join clc in ChangedLinesCounts on rev.Revision equals clc.Revision
    select new {rev.Revision, 
    rev.Date, usr.UserName, usr.ID, clc.LinesCount}).Take(6);

I make a couple of joins on different tables, not relevant for this question what keys are, but at the end of this query my result “table” contains

{Revision, Date, UserName, ID, LinesCount}

Now I execute e GroupBy in order to calculate a total lines count per user.

So..

from row in result group row by row.ID into g  {1}
    select new { 
        g.Key,
        totalCount = g.Sum(count=>count.LinesCount)
    };

So I get a Key=ID, and totalCount=Sum, but

Confusion

I would like to have also other fields in final result.
In my understanding “table” after {1} grouping query consist of

{Revision, Date, UserName, ID, LinesCount, TotalCount}

If my assumption is correct, why I can not do something like this:

from row in result group row by row.ID into g  {1}
        select new { 
            g.Key,
                g.Revision //Revision doesn't exist ! Why ??
            totalCount = g.Sum(count=>count.LinesCount)
        };

but

from row in result group row by row.ID into g  {1}
            select new { 
                g.Key,
                    Revision = g.Select(x=>x.Revision), //Works !
                totalCount = g.Sum(count=>count.LinesCount)
            };

Works !, but imo, sucks, cause I execute another Select.

Infact looking on LinqPad SQL output I get 2 SQL queries.

Question

Is there any elegant and optimal way to do this, or I always need to run Select
on groupped data, in order to be able to access the fields, that exists ?

  • 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-05-27T22:13:31+00:00Added an answer on May 27, 2026 at 10:13 pm

    The problem is, that you only group by ID – if you’d do that in SQL, you couldn’t access the other fields either…

    To have the other fields as well, you have to include them in you group clause:

    from row in result group row by new { row.ID, row.Revision } into g
        select new { 
            g.Key.ID,
            g.Key.Revision
            totalCount = g.Sum(count=>count.LinesCount)
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following query: var result = ( from role in db.Roles join user
I have defined the following variable var result = from report in firoozehDataContext.Reports select
I have this: var result = (from t in MyDC where t.UserID == 6
I have a LINQ query: var result = from mt in MessageTypes join mtfmt
If you have a simple Linq query like: var result = from record in
I have the following code: if (intval == 0) { var result = (from
Hi I have a Linq CodeBehind Function Like this var result = from m
if you have something like(just an example don't worry) var result = from n
I have a method: internal List<int> GetOldDoctorsIDs { var Result = from DataRow doctor
Assume I have the following query against the AdventureWorks DB: var result = from

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.