locked
Razor how to sum model data RRS feed

  • Question

  • User-1104215994 posted

    Hello guys,

    I need to get <g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="8" data-gr-id="8"><g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="7" data-gr-id="7">sum</g></g> of model data but couldn't manage how to implement it. Any help would be great?

    Thanks in advance.

    <div class="col-sm-3 c217">
            @if (Model.lines != null)
            {
                <h3 class="center">Results</h3>
                <table class="table table-bordered table-responsive table-hover" style="font-size: 11px !important;">
                    <thead>
                    <tr>
                        <th>Name </th>
                        <th>Master</th>
                        <th>Count </th>
                        <th>Average</th>
                        <th>Position</th>
                    </tr>
                    </thead>
                    @for (int i = 0; i < Model.lines.Count(); ++i)
                    {
                        for (int k = 0; k < Model.lines[i].products.Count(); ++k)
                        {
                            if (Model.lines[k].products != null)
                            {
                                <tr>
                                    <td>
                                        @Model.lines[i].products[k].name
                                    </td>
                                    <td>
                                        @Model.lines[i].products[k].master_count
                                    </td>
                                    <td>
                                        @Model.lines[i].products[k].count
                                    </td>
                                    <td>
                                        @Model.lines[i].products[k].count_average
                                    </td>
                                    <td>
                                        @Model.lines[i].products[k].position_average
                                    </td>
                                </tr>
                            }
                        }
                        <tr>
                            <td>
                                Total
                            </td>
                            <td>
                                @Model.Sum(Model.lines[i].products[k].master_count)
                            </td>
                        </tr>
                    }
                </table>
            }
    
    
        </div>

    Here is my model:

    public class ImageVm
        {
            public string ImageUrl { get; set; }
            public Line[] lines { get; set; }
        }
    
        public class Line
        {
            public int number { get; set; }
            public Product[] products { get; set; }
            public string products_position_average { get; set; }
            public string line_average { get; set; }
        }
    
        public class Product
        {
            public int id { get; set; }
            public string name { get; set; }
            public int count { get; set; }
            public int master_count { get; set; }
            public string count_average { get; set; }
            public string position_average { get; set; }
        }

    Wednesday, January 9, 2019 10:51 AM

Answers

All replies

  • User475983607 posted

    The linq syntax is...

    int masterCountTotalPerProduct = 0;
    using (StreamReader file = File.OpenText("data.json"))
    {
        JsonSerializer serializer = new JsonSerializer();
        JsonResponse response = (JsonResponse)serializer.Deserialize(file, typeof(JsonResponse));
        masterCountTotalPerProduct = response.lines[0].products.Sum(m => m.master_count);            
                        
    }

    Linq reference docs.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/getting-started-with-linq

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 9, 2019 2:52 PM
  • User-1104215994 posted

    Hi <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="9" data-gr-id="9">mgebhard</g>,

    Can't I use aggregate functions on View via model? Because <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="11" data-gr-id="11">model</g> is already on the view and it has all the data. <g class="gr_ gr_12 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="12" data-gr-id="12">Besides</g> I have to calculate 2 more sums. If your way is the only one, how can I send those all sums to the View in the controller?

    [HttpGet]
            public ActionResult FakeAnalyze(string image)
            {
                
                ImageVm vm = new ImageVm();
                //string img = string.Empty;
                using (StreamReader file = System.IO.File.OpenText(@"C:\PlanogramWebApp\PlanogramWebApp\images\test.json"))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    vm = (ImageVm)serializer.Deserialize(file, typeof(ImageVm));
                }
                // Convert byte[] to Base64 String
                string base64String = vm.ImageUrl;
                vm.ImageUrl = "data:image/png; base64," + base64String;
                return View("Index", vm);
            }

    edit: I got what you mean :)

    @Model.lines[i].products.Sum(m => m.master_count)

    Best Regards.

    Wednesday, January 9, 2019 6:08 PM