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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:28:12+00:00 2026-05-31T23:28:12+00:00

I have a problem with this query trying to get a sum and a

  • 0

I have a problem with this query trying to get a sum and a single value from the same child

Class 1

class Project{
    public int Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<SubProject> SubProjects { get; set; }
}

Class 2

class SubProject{
    public int Id { get; set; }
    public string Description { get; set; }
    public IEnumerable<Week> Weeks { get; set; }
}

Class 3

class Week{
    public int Week { get; set; }
    public int Hours { get; set; }
}

what i’m trying to do is get all weeks, within a range, for a Project in a new list of classes like this

class ProjectOverview{
    public int Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<ProjectWeekOverview> Weeks { get; set; }
}
class ProjectWeekOverview{
    public int Week { get; set; }
    public int TotalHours { get; set; }
}

i have tried something like this, but i cant manage to get in the week for it also, if what i done this far is even correct

List<Project> projects = { List.Of.Projects };
List<ProjectOverview> overview = projects
    .Select(p => new ProjectOverview
    {
        Id = p.Id,
        Name = p.Name,
        Weeks = p
            .SubProjects
            .Select(sp => new ProjectWeekOverview
            {
                Week = ????,
                TotalHours = sp
                    .Where(w => w.Week >= 30 && w.Week <= 35)
                    .Sum(w => w.Hours)
            })
    })
    .ToList();

anyone that can help with this?

Edit: a subproject can contain multiple week items with the same week like, to store hours from different action how long they took

week { week = 10, hours = 3 }
week { week = 10, hours = 4 }
week { week = 11, hours = 3 }

so what i want is to have the week property to a set week, and the totalhours to sum of all hours in that week

  • 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-31T23:28:14+00:00Added an answer on May 31, 2026 at 11:28 pm

    You need to GroupBy week, and have Week as the key and Sum as item.

    Give this a try. The output is an anonymous type, must have to convert it to whatever type you want as result. Its the grouping that matters.

            List<Project> projects = null;
    
            var weeksAndHours = projects
                .Select(p => new 
                {
                    Id = p.Id,
                    Name = p.Name,
                    Weeks = p
                        .SubProjects.SelectMany(sp => sp.Weeks)
                        .Where(w => w.Week >= 30 && w.Week <= 35)
                        .GroupBy(w => w.Week)
                        .Select(g => new { week = g.Key, hours = g.Sum( w=> w.Hours) })
                });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get values from xml, and I have problem with empty value.
I seem to have a problem with this SQL query: SELECT * FROM appts
I'm trying to get all products from the database with a single query. I
I have been trying to get this query to work but I am stumbed
I have a problem trying to get the count out of the following query:
I have a problem with an SQL query on Postgresql. This select clause is
I have a nHibernate query like this ICriteria query = session.CreateCriteria(typeof(MyResult)) .Add(Expression.Eq(ResultTypeId, myResult.ResultTypeId)) Problem
I have problems running a dynamic LIKE statement in my project: this query works
I have a query like this: SELECT id, user_id FROM orders GROUP BY user_id
I'm trying to get the following query to work in Hibernate: SELECT m 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.