Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Sum of relevant entries' balances from third level model - LINQ

$
0
0

Hi,

In an ASP Core project I have a Customer's Model as:

using System;
using System.Collections.Generic;

namespace RiskDotNet.Models
{
    public partial class Customers
    {
        public Customers()
        {
            Accounts = new HashSet<Accounts>();
        }

        public string SrcSys { get; set; }
        public string CustId { get; set; }
        public string CustNm { get; set; }

        public virtual ICollection<Accounts> Accounts { get; set; }
    }
}

The Accounts' Model pertaining to each Customer is:

using System;
using System.Collections.Generic;

namespace RiskDotNet.Models
{
    public partial class Accounts
    {
        public Accounts()
        {
            Balances = new HashSet<Balances>();
        }

        public string SrcSys { get; set; }
        public string CustId { get; set; }
        public string AccId { get; set; }
        public string ProdId { get; set; }

        public virtual ICollection<Balances> Balances { get; set; }
        public virtual Customers Customers { get; set; }
    }
}

Finally the third model to contain and reflect the balances (transactions) pertaining to each Account as follows:

using System;
using System.ComponentModel.DataAnnotations;

namespace RiskDotNet.Models
{
    public partial class Balances
    {
        [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
        public DateTime RepDt { get; set; }
        public string CustId { get; set; }
        public string AccId { get; set; }
        public string SrcSys { get; set; }
        public decimal? PrOs { get; set; }

        public virtual Accounts X { get; set; }
    }
}

The Details portion for the CustomersController:

        public async Task<IActionResult> Details(string _SrcSys, string _CustId)
        {
            if (_SrcSys == null || _CustId == null)
            {
                return NotFound();
            }

            var customer = await _context.Customers
                .Include(Acc => Acc.Accounts)
                .SingleOrDefaultAsync(m => m.SrcSys == _SrcSys && m.CustId == _CustId);
            if (customer == null)
            {
                return NotFound();
            }

            return View(customer);
        }

How to include the Sum of Customer>Account>Balance's field PrOs in the LINQ?

Now on the Details page of the Customers the view is:

@model RiskDotNet.Models.Customers
@{ViewData["Title"] = "Details";}<div><FAQCSS_Head><h4>@Html.DisplayFor(model => model.CustNm)</h4></FAQCSS_Head><dl class="FAQCSS_dl-horizontal"><dt>Source System</dt><dd>@Html.DisplayFor(model => model.SrcSys)</dd><dt>Customer ID</dt><dd>@Html.DisplayFor(model => model.CustId)</dd></dl></div><br /><div style="position: relative; left:300px; top: -80px; width: 900px"><dl><dd><table class="table"><tr><th>Acc. ID</th><th>Prod. ID</th><th>Pr. O/s.</th></tr>
                @foreach (var item in Model.Accounts)
                {<tr><td>
                           @Html.DisplayFor(modelItem => item.AccId)</td><td>
                            @Html.DisplayFor(modelItem => item.ProdId)</td><td>
                            ????????</td></tr>
                }</table></dd></dl></div><a asp-action="Index">Customers' List</a>

All I need is to have some appropriate linq and amendment in the view at ???????? place to reflect the sum of account's balances on customer'd details page. Any idea?

In simple words, when one selects a customer's details page, he finds the customer details plus the list/table of accounts pertaining to him but I need the said list to also reflect the SUM of the PrOs of each account as per the balances data.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>