Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Inmedia 124 posts 176 karma points
    Jan 19, 2016 @ 10:46
    Inmedia
    0

    How to get sum of property values from children?

    Hi people

    I need to get the sum of a property value from all children.

    Can anyone tell me how this is done with Razor?

    I would think it is pretty simple task, but I am relatively new to razor scripting :)

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 19, 2016 @ 11:05
    Dennis Aaen
    0

    Hi Inmedia,

    So you should loop though some children, and then show the data on the page above those children.

    Then something like the razor blow should work for you.

    @{ var selection = CurrentPage.Children.Where("Visible"); }
    
    @if (selection.Any())
    {
        <ul>
            @foreach (var item in selection)
            {
                <li>
                    <a href="@item.Url">@item.Name</a>
                </li>
            }
        </ul>
    }
    

    What you need to do is to call custom properties is:

    @item.PropertyAlias
    

    Inside the foreach loop

    Hope this helps, and let me know if I have misunderstood your question.

    /Dennis

  • Inmedia 124 posts 176 karma points
    Jan 19, 2016 @ 11:13
    Inmedia
    0

    Hi Dennis

    I think I wasn't precise enough in my post.... What I need is for razor to calculate the sum of a numeric property that all children has.

    I have a table that list all the children and their values. Each child gets a row... This I have already set up using the method you posted.

    But in the bottom of this table, I need a sum function that calculates the total of all the child values.

    Hope this makes sense? :)

    I have made an image to visualise what I need :) enter image description here

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 19, 2016 @ 12:16
    Nik
    0

    Hi Inmeida,

    How far have you got? Have you managed to render out the table without the sum?

    If so, can you share your razor code so we can get a feel for what you are doing?

    Thanks,

    Nik

  • Inmedia 124 posts 176 karma points
    Jan 19, 2016 @ 12:35
    Inmedia
    0

    Hi Nik

    Yes, that is correct. It's the "sum" part I can't figure out :) I need the sum of the properties called "antalVisnigner"...

    Here is my script:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    
    @* Ensure that the Current Page has children, where the property umbracoNaviHide is not True *@
    @if (Model.Children.Where("Visible").Any())
    {
        <tbody>            
            @* For each child page under the root node, where the property umbracoNaviHide is not True *@
            @foreach (var childPage in Model.Children.Where("Visible"))
            {
                <tr>
                    <td>@childPage.Name</td>
                    <td>
                        @* Format numeric value with punctuation *@
                        @if (childPage.HasValue("antalVisnigner")){ 
                            int price = childPage.antalVisnigner;
                            string formattedPrice = price.ToString("#,##0");
    
                            @formattedPrice     
                        }
                    </td>
                    <td>@childPage.antalKlik</td>
                    <td>@childPage.cTR%</td>
                    <td>@childPage.avgCPM</td>
                    <td>@childPage.cost</td>
                </tr>
            }
        </tbody>
    
        <tfoot>  
            <tr>
                <th>Total</th>
                <th>Sum</th>    @* <-----  This is what I can't figure out *@
                <th> </th>
                <th> </th>
                <th> </th>
                <th> </th>
            </tr>
    
        </tfoot>
    }
    
  • Rick 1 post 21 karma points
    Jan 19, 2016 @ 12:56
    Rick
    0

    Hee Inmedia,

    I think you can try something like this:

    @Model.Childeren.Where("Visible").Sum(p => p.antalVisnigner);
    

    Cheers,

  • Inmedia 124 posts 176 karma points
    Jan 19, 2016 @ 13:02
    Inmedia
    0

    Hi Rick

    I get the following error, when I try that:

    Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
    
  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 19, 2016 @ 13:00
    Nik
    0

    Hi Inmedia,

    Rick's option is one possible route, however it does mean querying your fields multiple times. This shouldn't be a problem but this is an alternative:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @* Ensure that the Current Page has children, where the property     umbracoNaviHide is not True *@
    @if (Model.Children.Where("Visible").Any())
    {
        int sum = 0;
    <tbody>            
        @* For each child page under the root node, where the property umbracoNaviHide is not True *@
        @foreach (var childPage in Model.Children.Where("Visible"))
        {
            <tr>
                <td>@childPage.Name</td>
                <td>
                    @* Format numeric value with punctuation *@
                    @if (childPage.HasValue("antalVisnigner")){ 
                        int price = childPage.antalVisnigner;
                        string formattedPrice = price.ToString("#,##0");
                        sum  =+ price;
                        @formattedPrice     
                    }
                </td>
                <td>@childPage.antalKlik</td>
                <td>@childPage.cTR%</td>
                <td>@childPage.avgCPM</td>
                <td>@childPage.cost</td>
            </tr>
        }
    </tbody>
    
    <tfoot>  
        <tr>
            <th>Total</th>
            <th>Sum: @sum  </th>    @* <-----  This is what I can't figure out *@
            <th> </th>
            <th> </th>
            <th> </th>
            <th> </th>
        </tr>
    
    </tfoot>
    }
    
  • Inmedia 124 posts 176 karma points
    Jan 19, 2016 @ 13:11
    Inmedia
    0

    Hi Nik

    But that just sets the sum to be 0.... Right?

    That's what I get when I use your script. It doesn't do any math for me, as far as I can see.

    Do I need to add something else to the script, to make it work?

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 19, 2016 @ 13:21
    Nik
    1

    I think I got my sum =+ price back to front, it might need to be sum += price, or even sum = sum + price.

  • Inmedia 124 posts 176 karma points
    Jan 19, 2016 @ 13:26
    Inmedia
    0

    Eureka!!
    Yes, that was it :)

    Big high five to you! :)

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 19, 2016 @ 16:09
    Nik
    0

    No problem, Dan's answer below is another good option as well. It's a good idea to mark the answer in a thread so others know there is a valid answer there if they are having the same issue.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jan 19, 2016 @ 13:22
    Dan Diplo
    1
    int summed = Model.Children().Sum(x => x.GetPropertyValue<int>("antalVisnigner"));
    
Please Sign in or register to post replies

Write your reply to:

Draft