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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:16:18+00:00 2026-06-14T17:16:18+00:00

Suppose I have a database defined as: // Baked goods vendors var vendor =

  • 0

Suppose I have a “database” defined as:

// Baked goods vendors
   var vendor = new[] {
    new { ID = 1, Baker = "Pies R Us", StMnemon = "NY", Items = 8, Rating = 9 },
    new { ID = 2, Baker = "Mikes Muffins", StMnemon = "CA", Items = 5, Rating = 9 },
    new { ID = 3, Baker = "Best Bakers", StMnemon = "FL", Items = 2, Rating = 5 },
    new { ID = 4, Baker = "Marys Baked Treats", StMnemon = "NY", Items = 8, Rating = 7 },
    new { ID = 5, Baker = "Cool Cakes", StMnemon = "NY", Items = 4, Rating = 9 },
    new { ID = 6, Baker = "Pie Heaven", StMnemon = "CA", Items = 12, Rating = 9 },
    new { ID = 7, Baker = "Cakes N More", StMnemon = "GA", Items = 6, Rating = 8 },
    new { ID = 8, Baker = "Dream Desserts", StMnemon = "FL", Items = 2, Rating = 7 }
};

// Locations
var location = new[] {
    new {ID= 1, State = "New York", Mnemonic = "NY"},
    new {ID= 2, State = "Massachusetts", Mnemonic = "MA"},
    new {ID= 3, State = "Ohio", Mnemonic = "OH"},
    new {ID= 4, State = "California", Mnemonic = "CA"},
    new {ID= 5, State = "Florida", Mnemonic = "FL"},
    new {ID= 6, State = "Texas", Mnemonic = "TX"},
    new {ID= 7, State = "Georgia", Mnemonic = "GA" }
};

I want to build a query that would be the equivalent of the SQL query:

SELECT   State, Rating, SUM(Items) AS 'Kinds'
FROM     vendor, location
WHERE    vendor.StMnemon = location.Mnemonic
GROUP BY State, Rating

Two things of interest in this query are:

  1. The GROUP BY involves multiple tables, and
  2. The result contains a summation of a column not appearing in the grouping criteria.

I’ve seen the solutions in the posts on grouping by multiple tables and summing columns not in the group-by. The problem is that combining both doesn’t really duplicate the relational query.

I try to duplicate it in LINQ with the following code:

var query = from v in vendor
           join l in location
           on v.StMnemon equals l.Mnemonic
           orderby v.Rating ascending, l.State
           select new { v, l };

var result = from q in query
           group q by new { 
               s = q.l.State, 
               r = q.v.Rating
/* ==> */    , i = q.v.Items
           } into grp
        select new 
        {
            State = grp.Key.s,
            Rating = grp.Key.r
/* ==> */ , Kinds = grp.Sum(k => grp.Key.i)
        };

This results in:

=================================
State           Rating  Kinds
Florida         5       2
Florida         7       2
New York        7       8
Georgia         8       6
California      9       5
California      9       12
New York        9       8
New York        9       4
=================================

Whereas, the SQL query given above gives this result:

=========================
State       Rating  Kinds
Florida     5       2
Florida     7       2
New York    7       8
Georgia     8       6
California  9       17
New York    9       12
=========================

The discrepancy is because there seems to be no place to put additional columns, other than in the grouping criteria, which of course changes the grouped result. Commenting out the two lines indicated by the /* ==> */ comment in the code above will give the same grouping as the SQL result, but of course that removes the summation field that I want to include.

How do we group multiple tables in LINQ and include additional criteria without changing the grouped result?

  • 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-14T17:16:19+00:00Added an answer on June 14, 2026 at 5:16 pm

    something like this seems to return the same as the SQL query:

    var result = from v in vendor
                 from l in location
                 where l.Mnemonic == v.StMnemon
                 group v by new { l.State, v.Rating } into grp
                 orderby grp.Key.Rating ascending, grp.Key.State
                 select new {State = grp.Key.State, Rating = grp.Key.Rating, Kinds = grp.Sum(p=>p.Items)};
    
    foreach (var item in result)
            Console.WriteLine("{0}\t{1}\t{2}", item.State, item.Rating, item.Kinds);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two queries on a database table. The queries are defined in
Suppose I have a database table with columns a, b, and c. I plan
Suppose I have a database like this: This is set up to give role-wise
Suppose that I have a database with student information: {'student_name' : 'Alen', 'subjects' :
I was recently pondering the following scenario: suppose you have a huge database and
Suppose I have 2 tables in a database. eg: Dog & Boss This is
Suppose the following: I have a database set up on database.mywebsite.com , which resolves
Suppose I have two tables on a database, T 10 and T 11 ,
Suppose I have an array that mimics a database table. Each array element represents
Suppose I have an Orders table in my database and a corresponding model class

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.