I have a database where I store Orders of which the model looks as follows:
public class Order
{
public int OrderId { get; set; }
public int ProductId { get; set; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public int UserId { get; set; }
public DateTime Date { get; set; }
public bool IsConfirmed { get; set; }
public bool IsSettledWithSalary { get; set; }
public virtual Product Product { get; set; }
public virtual User User { get; set; }
}
So the orders are linked to product (for every product ordered there a new order is made) and to the user that did the order. The property IsConfirmed is true if someone's order is confirmed and IsSettledWithSalary is true if the specific order has been deduced from the salary.
Now I want to make a table that shows the balance and name of a user, i.e., display the name of the user with after it the sum of the orders that are Confirmed but not yet deduced from the salary.
To do this I send the following tuple to the view in the controller:
var users = db.Orders.Include(o => o.User)
.Where(o => o.IsConfirmed == true &&
o.IsSettledWithSalary == false)
.GroupBy(order => order.UserId)
.Select(user => user.FirstOrDefault());
var balances = db.Orders
.Where(order => order.IsConfirmed == true &&
o.IsSettledWithSalary == false)
.GroupBy(order => order.UserId)
.Select(group => group.Sum(order => order.UnitPrice
* order.Quantity) );
return View(Tuple.Create(users.ToList(), balances.ToList()));
Then in the view I picked up the tuple by using the model
@model Tuple<List<Models.Order>, List<Decimal>>
and accessing them with Model.Item1 and Model.Item2, and using a foreach loop for setting every user next to the salary in a table.
I find this solution pretty ugly and possibly error prone so that is why I want to create a better query / lambda expression to send to the view.
Is it possible to just send the usernames and balance to the view without making a tuple? And if so, how?