我可以一次将一个属性和另一个属性的总和发送到视图吗? [英] Can I send a property and the total sum of another property to the view at once?

查看:58
本文介绍了我可以一次将一个属性和另一个属性的总和发送到视图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,用于存储模型如下的订单:

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; }        
}

因此,订单链接到产品(为每个订购的产品创建一个新订单),并链接到执行该订单的用户.如果已确认某人的订单,则属性IsConfirmed为true;如果已从工资中推导出特定的订单,则IsSettledWithSalary为true.

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>>

并使用Model.Item1和Model.Item2访问它们,并使用foreach循环在表中的薪水旁边设置每个用户.

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.

我发现此解决方案非常丑陋,并且可能容易出错,因此这就是为什么我想创建一个更好的查询/lambda表达式以发送到视图的原因.

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?

推荐答案

使用"ViewModel",或者您可以计算该属性,然后将其粘贴在ViewBag/ViewData上

Use a "ViewModel" or you could calculate the property and then stick it on ViewBag/ViewData

ViewModel示例:

Example ViewModel:

class OrderTotalViewModel
{
    public string User {get;set;}
    public decimal Total {get;set;}
}

人口:

var users = db.Orders.Include(o => o.User)
           .Where(o => o.IsConfirmed == true && 
                                        o.IsSettledWithSalary == false)
           .GroupBy(order => order.UserId)
           .Select(group => new OrderTotalViewModel
           {
                User = u.First().Name, // or u.FirstName etc...
                Total = u.Sum(order => order.UnitPrice * order.Quantity)
           });

视图中:

Name: @Model.User
Total: @Model.Total

这篇关于我可以一次将一个属性和另一个属性的总和发送到视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆