user6084053
user6084053

Reputation:

Linq a list in a Modal and send the value

Well i am trying a new approach and want to do this:

Model Balance

 public class Balance
{
    public Balance()
    {
        Expenses = new List<Expenses>();
        Earning = new List<Earnings>();
        value = 0;
    }

    public int BalanceId { get; set; }
    public virtual List<Expense> Expenses { get; set; }
    public virtual List<Earning> Earnings { get; set; }
    public string ApplicationUserId { get; set; }

    public decimal value { get; set; }
}

I have this model, and i wanted to know if it is possible to do a query in Linq to sum all the values from the list of Expenses and earnings and how can i do that,i need this approach cause in my program now, i am doing the loops in the view like this:

Controller

 public PartialViewResult _ObtemSaldo()
    {
        var userId = User.Identity.GetUserId();
        var balance = db.Balance.Where(d => d.ApplicationUserId == userId).FirstOrDefault();
        return PartialView(balance);
    }

View

@model MSDiary.Models.Balance



@helper getBalance()
{
decimal balance = 0;
int id = Model.BalanceId;
if (Model != null)
{
    foreach (var item in Model.Despesas)
    {
        balance-= item.ExpenseValue;
    }
    foreach (var item in Model.Rendimentos)
    {
        balance += item.EarningValue;
    }
    Model.value = balance;
    if(balance < 0)
    {
        ViewBag.color = "red";
                 <p style="color:red">@saldo</p>
            }
    else
    {
        ViewBag.color = "green";
                 <p style="color:green">@saldo</p>
    }
}

}

<h3>Actual balance: <span>@getBalance()</span></h3>

I want to know if i am thinking well trying to change the approach, and how can i do the linq query to get what i need

Ps: Sorry for my bad English

Upvotes: 3

Views: 473

Answers (2)

Fabjan
Fabjan

Reputation: 13676

Although your code is ok but i'd move business logic to server side instead. We can create helper (extension) method there. Let's use LINQ (as Stephen Muecke suggested) :

public static class BalanceHelper
{
   public static Balance GetBalance(this Balance balance)
   {
      if(balance != null)
      {         
         balance.value = balance.Earnings.Sum(x => x.EarningValue) - 
                           balance.Expenses.Sum(x => x.ExpenseValue);
      }

      return balance; 
   }
}

And you can use it in controller after adding namespace that contains it :

 using MyProj.Core.MyHelpers;  // namespace with BalanceHelper class

 public PartialViewResult _ObtemSaldo()
 {
    var userId = User.Identity.GetUserId();
    var balance = db.Balance.Where(d => 
             d.ApplicationUserId == userId)
                  .FirstOrDefault()
                  .GetBalance();  // boom

    return PartialView(balance);
 }

And in view we can do UI operations :

@helper highlightBalance()
{
    if(Model.value < 0)
    {
        ViewBag.color = "red";
                  <p style="color:red">@saldo</p>
    }
    else
    {
        ViewBag.color = "green";
                 <p style="color:green">@saldo</p>
    }
}

<h3>Actual balance: <span>@highlightBalance()</span></h3>

Upvotes: 0

wertzui
wertzui

Reputation: 5730

implement value as a getter-only property, because code that computes something does not belong in the view:

public decimal value => Earnings.Sum(e => e.EarningValue) - Expenses.Sum(e => e.ExpenseValue);

If you are not using C# 6, do it this way:

public decimal value { get { return Earnings.Sum(e => e.EarningValue) - Expenses.Sum(e => e.ExpenseValue); } }

Upvotes: 0

Related Questions