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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:12:20+00:00 2026-05-18T00:12:20+00:00

I would like sum a property on a sub list of a list …

  • 0

I would like sum a property on a sub list of a list … example:

I have:

public class List1 {
public List<List2> List2 { get; set; }
}

public class List2 {
public int TotalUnits { get; set; }
}

In my view I have a List<List1>, so that I want to do something like:

<%= Model.List1.Where(x.List2.Any()).Sum(x=>x.TotalUnits) .%>

Obviously everything after Sum doesn’t work, but is there a way I can do this in a single line without having to create a new Dto on my server side?

  • 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-18T00:12:21+00:00Added an answer on May 18, 2026 at 12:12 am

    Are you trying to get the sum of all of the TotalUnits in every List2 instance? If so then do the following

    <%= Model.List1.SelectMany(x => x.List2).Sum(x => x.TotalUnits) %>
    

    Here’s how this works

    Model.List1.SelectMany(x => x.List2)
    

    This takes the List<List1> and transforms it into an IEnumerable<List2>. SelectMany is similar to Select in that it’s a projection for elements in an enumeration except that it expects an enumerable result. The collection of enumerable results is then put into a single enumerable. Or in simpler words, it flattens a list of lists.

    .Sum(x => x.TotalUnits)
    

    This just sums the TotalUnits member of List2.

    There is also a small typo in your code. I believe it should read as follows (note the missing class keyword)

    public int TotalUnits { get; set;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sum( int(i.replace(',',''))if re.search('\d',i)!=None for i in list) I would like to sum all elements
I would like to perform something similar to this (ie get the sum of
I have an object model like this: public class Quantity { public decimal Weight
I would like do some thing like this: SELECT sum(quantity) FROM orders WHERE order_code
I would like to do the following: SELECT sum(max(field-10,000,0)) from TABLE as in, I
i would like to know whether the following query is optimized? select SUM(payments.paid_amt) as
Would like to be able to set colors of headings and such, different font
I have a list of recipes obtained from a database that looks like this:
I have a question very similiar to another question: Get name of property as
I have the following information var details = Details.Where(d => d.isActive); I would like

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.