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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:15:10+00:00 2026-06-12T08:15:10+00:00

Having a little trouble constructing a linq statement (C#, query syntax but I could

  • 0

Having a little trouble constructing a linq statement (C#, query syntax but I could handle method sytax). It needs to support grouping as well as a exposing a calculated property. I have a handle on each individually but can’t get them to seem to work together. Any help would be appreciated.

Data

GrpField          Qty      Price
-------------     ---      -----
RED                1        10
RED                2        10
RED                1        50
BLUE               2        30
BLUE               2        50

Output needs to be sum of Total Price (Qty * Price) for each group

Desired Result

RED    80
BLUE  160

This gets me the Total Price line

from x in origdata
let linetotal = x.qty * x.price
select new { x.grpfield, x.qty, x.price, linetotal } 

But I am unable to group by the new anonymous type since you can’t run aggregates like sum on anonymous types. If I run the group first, I can’t figure out how to work the linetotal into the mix BEFORE the aggregate sum.

  • 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-12T08:15:12+00:00Added an answer on June 12, 2026 at 8:15 am

    As evidence against your since you can’t run aggregates like sum on anonymous types statement…

    var data = new[]
        {
            new {GrpField = "RED", Qty = 1, Price = 10},
            new {GrpField = "RED", Qty = 2, Price = 10},
            new {GrpField = "RED", Qty = 1, Price = 50},
            new {GrpField = "BLUE", Qty = 2, Price = 30},
            new {GrpField = "BLUE", Qty = 2, Price = 50},
        };
    
    var grouped =
        from d in data
        group d by d.GrpField
        into g
        select new {Group = g.Key, Sum = g.Sum(x => x.Qty * x.Price)};
    
    foreach(var g in grouped)
        Console.WriteLine("{0} - {1}", g.Group, g.Sum);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

having a little trouble, how could I alter this mysql query so that if
Im having a little trouble constructing a query. I have a table with 3
I'm having a little trouble with a SQL query, and thought I'd solicit the
I'm having a little trouble getting this query to work: $userId = mysql_real_escape_string( $_SESSION['user_id']
I'm having a little trouble about using multiple Left Joins in a query. Some
i'm having a little trouble with an elseif MySQL query i'm using to search
I'm having a little trouble making an SQL SERVER 2000 query. Here is my
I'm having a little trouble deciding the best way to refactor a method which
I'm having a little trouble with my MYSQL query I have a DB full
I'm having a little trouble constructing the regular expression using java. The constraint is,

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.