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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:10:24+00:00 2026-06-14T06:10:24+00:00

I have a data set that looks like this: [ { Size : Small,

  • 0

I have a data set that looks like this:

[
{
 "Size" : "Small",
 "Details" :
  {
    "Detail 1" : 1.0,
    "Detail 1" : 1.0,
    "Detail 2" : 1.0,
  }
},
{
 "Size" : "Small",
 "Details" :
  {
    "Detail 1" : 2.0,
    "Detail 1" : 3.0,
    "Detail 2" : 4.0,
  }
},
{
 "Size" : "Medium",
 "Details" :
  {
    "Detail 1" : 1.0,
    "Detail 1" : 1.0,
    "Detail 2" : 1.0,
    "Detail 3" : 1.0,
  }
},
//... etc
]

For all items with the same “Size”, I’d like to individually sum up the matching “Detail” entries, and then average them across like “Size”d items. i.e.:

[
{
 "Size" : "Small",
 "Details" :
 {
    "Detail 1" : 3.5,
    "Detail 2" : 2.5,     // Average of the summation of the two 'small' items in original data set
 },
    {
     "Size" : "Medium",
     "Details" :
      {
        "Detail 1" : 2.0, // Average of the two details for medium.
        "Detail 2" : 1.0,
        "Detail 3" : 1.0,
      }
    },
]

The code I’ve got is such, but I’m stuck in figuring out how to average across the nested set. Any pointers would be appreciated.

My code, thus far.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{

    class ItemWithDetails
    {
        public string Size;
        public List<KeyValuePair<string, double>> Details { get; private set; }

        public ItemWithDetails(string size)
        {
            Size = size;
            Details.Add(new KeyValuePair<string, double>("Detail 1", 1));
            Details.Add(new KeyValuePair<string, double>("Detail 1", 1));
            Details.Add(new KeyValuePair<string, double>("Detail 2", 1));
            Details.Add(new KeyValuePair<string, double>("Detail 2", 1));
            Details.Add(new KeyValuePair<string, double>("Detail 2", 1));
            Details.Add(new KeyValuePair<string, double>("Detail 3", 1));

            if (size == "Large")
            {
                Details.Add(new KeyValuePair<string, double>("Detail 3", 1));
                Details.Add(new KeyValuePair<string, double>("Detail 3", 1));
                Details.Add(new KeyValuePair<string, double>("Detail 3", 1));
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var testData = new List<ItemWithDetails>()
            {
                new ItemWithDetails("Small"),
                new ItemWithDetails("Small"),
                new ItemWithDetails("Medium"),
                new ItemWithDetails("Medium"),
                new ItemWithDetails("Medium"),
                new ItemWithDetails("Large"),
                new ItemWithDetails("Large"),
                new ItemWithDetails("Large"),
            };

            // Trying to get the average of each detail, per size.

            var detailSummed = from item in testData
                               select new
                               {
                                   size = item.Size,
                                   detailsSummed = from detail in item.Details
                                                   group detail by detail.Key into detailGroup
                                                   select new
                                                   {
                                                       detailName = detailGroup.Key,
                                                       detailSum = detailGroup.Sum(a => (a.Value))
                                                   }
                               };

            var averageAcrossItems =    from item in detailSummed
                                        group item by item.size into itemGroup
                                        select new 
                                        {
                                            size = itemGroup.Key,
                                            detailsAveraged = // not sure how I can average across items, while at this level.  Do I have to flatten it?
                                        }

        }
    }
}

UPDATE:

Using Adam Mills code, I’ve gotten closer, combining two separate LINQ Queries. Can this be made into one LINQ query that’s hopefully more readable?

        var detailSummed = from item in testData
                           select new
                           {
                               size = item.Size,
                               detailsSummed = from detail in item.Details
                                               group detail by detail.Key into detailGroup
                                               select new
                                               {
                                                   detailName = detailGroup.Key,
                                                   detailSum = detailGroup.Sum(a => (a.Value))
                                               }
                           };

        var test2 = detailSummed.GroupBy(x => x.size)
                                .Select(y =>
                                           new
                                           {
                                               Size = y.Key,
                                               DetailAverages = y   .SelectMany(x => x.detailsSummed)
                                                                    .GroupBy(x => x.detailName)
                                                                    .Select(x => new KeyValuePair<string, double>(x.Key, x.Average(c => c.detailSum)))
                                           });
  • 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-14T06:10:26+00:00Added an answer on June 14, 2026 at 6:10 am

    The following produces your desired output given your input, except it sums the two detail1 values for medium:

    var output = input
        .Select(iwd => new { Size = iwd.Size, Sums = iwd.Details.GroupBy(kvp => kvp.Key).Select(g => new { Detail = g.Key, Sum = g.Sum(kvp => kvp.Value) }) })
        .GroupBy(ds => ds.Size)
        .ToDictionary(g => g.Key, g => g.SelectMany(ds => ds.Sums).GroupBy(ds => ds.Detail).ToDictionary(dsg => dsg.Key, dsg => dsg.Sum(ds => ds.Sum) / g.Count()));
    

    Note it produces a Dictionary<string, Dictionary<string, int>>.

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

Sidebar

Related Questions

I have a data set that resembles this: id product_id size color price created_date
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
Say you want to take CMU's phonetic data set input that looks like this:
I have a data set that that I would like to call in a
I have a data set of item difficulties that correspond to items on a
Let's say I have a manager that looks something like this: public class CustomerManager
I have a WCF service that is hosted locally. My web.config looks like this:
I have a (dense) dataset that consist of 5 groups, so my data.frame looks
I have a data set that is around 700 rows with eight columns of
I have the following data set that I am trying to plot with ggplot2,

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.