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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:12:06+00:00 2026-06-14T12:12:06+00:00

I have two classes: public GeneralClassName { public GeneralClassName () { SpecificList = new

  • 0

I have two classes:

public GeneralClassName 
{
    public GeneralClassName ()
    {
        SpecificList = new List<OtherClass>();
    }
    public string StringValue;
    public string OtherStringValue;
    public List<OtherClass> SpecificList;
}

and

public OtherClass
{
    public string Name;
    public string Number;
}

After a JSON deserialization I obtain a nice List<GeneralClassName>, the result I want is a Dictionary<string, int> whose value is the sum of the variabiles “Number” inside List<OtherClass> inside List<GeneralClassName>, while the key is the variabile Name.

In other words I’d like to sum Number grouping by Name.

Now, the only thing that came across my mind is a nested foreach, something like that:

Dictionary<string, int> resultDictionary = new Dictionary<string, int>();
foreach(List<OtherClass> listOtherClass in bigListGeneralClass.Select(x => x.SpecificList))
{
    foreach(OtherClass otherClass in listOtherClass)
    {
        int value = 0;
        if(resultDictionary.ContainsKey(otherClass.Name))
        {
            resultDictionary[otherClass.Name] += otherClass.Number;
        }
        else
        {
            resultDictionary.Add(otherClass.Name, otherClass.Number);
        }
    }
}

While this solution seems to work well, I don’t like it at all.
Is there a more clean way to find this result? Maybe through a nice LINQ query?

  • 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-14T12:12:07+00:00Added an answer on June 14, 2026 at 12:12 pm

    As you don’t use any information from the GeneralClassName you can use SelectMany to flatten your list. This flat list of OtherClass instances is than grouped by the Name property. Finally, the list of groups is transformed into a dictionary with the key of the group (aka the Name property) being the key of the new property and the value being the sum of all Number values in that group:

    var result = bigListGeneralClass.SelectMany(x => x.SpecificList)
                                    .GroupBy(x => x.Name)
                                    .ToDictionary(x => x.Key,
                                                  x => x.Sum(y => y.Number));
    

    This code assumes that OtherClass.Number is in fact an int not a string. This assumption is also used in your sample code with the loop.
    If this assumption is not correct, change y.Number to int.Parse(CultureInfo.InvariantCulture, y.Number).
    Note: This will throw an exception if any of the numbers can’t be parsed, so you might want to make sure beforehand that all contain valid numbers.

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

Sidebar

Related Questions

I have two classes: public class RichMan { public string ID {get;set;} List<Car> _cars=new
I have two classes: public class Artikel { int id{get;set;}; string name{get;set;}; } public
I have two simple classes public class Blog { public Blog(){ Comments=new List<Comment>(); }
I have two classes: public class Person { public int Id{get;set;} public string Name{get;set;}
I have two classes: class MyClassInfo { public: void AddMethod(std::string name, void* pointer); //
I have two classes public class PrepaidPackage { private String name; private String serviceClassID;
I have two classes class A { public string something { get; set; }
I have the following two classes: public class Address { public string AddressLine1 {
Supposed I have two classes like this: public class Parent { public string Id
I have two classes: class Player { public string Id { set; get; }

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.