0

场景:视图应该有所有利润的总和,所有成本的总和,余额=利润-成本

public ProfitAndCostViewModel getTotalBalance()
    {
        var totalProfit = db.Profits.Where(p=>p.IdUser.UserId == WebSecurity.CurrentUserId).Sum(p=>p.Value);

        var totalCost = db.Costs.Where(c=>c.IdUser.UserId == WebSecurity.CurrentUserId).Sum(c=>c.Value);

        var balance = totalProfit - totalCost ;
        return new ProfitAndCostViewModel { FinalBalance = balance };
    }

控制器:

 public ActionResult Index()
        {


      var pcv = new ProfitAndCostViewModel();
          pcv.ProfModel =getProfitSum();
            pcv.CostModel =getCostSum();
            pcv.TOTALBALANCE = getTotalBalance();
             return View(pcv);


        }

看法:

@model WHFM.ViewModels.ProfitAndCostViewModel
@Model.FinalBalance.FinalBalance
4

4 回答 4

1

尝试

public ProfitAndCostViewModel getTotalBalance()
{
    var totalProfit = db.Profits.Where(p=>p.IdUser.UserId == WebSecurity.CurrentUserId).Sum(p=>p.Value);

    var totalCost = db.Costs.Where(c=>c.IdUser.UserId == WebSecurity.CurrentUserId).Sum(c=>c.Value);

    var balance = totalProfit - totalCost ;
    return new ProfitAndCostViewModel{ FinalBalance = balance};
}

据我所知,您只是在寻找当前用户的最终余额,对吗?如果是这样,则不需要返回 ProfitAndCostViewModel 的 IEnumerable。

如果您想要多对多用户余额输出(ProfitAndCostViewModel 的 IEnumerable),请尝试:

 public IEnumerable<ProfitAndCostViewModel> getTotalBalance()
 {
    var result = from p in db.Profits
                       group p by p.IdUser.UserId
                       into g
                       join c in db.Costs on g.Key equals c.IdUser.UserId
                       group new {g,c} by g.Key into h
                       select new ProfitAndCostViewModel
                           {
                               FinalBalance = h.Select(x=>x.g.Sum(y=>y.Value)).First() - h.Sum(x=>x.c.Value),
                                                       UserId = h.Key
                           };

    return result;
}

我只在列表上测试了这个,而不是数据库变量,所以它可能不起作用。

于 2013-03-15T13:31:17.103 回答
1

试试这个:

场景:视图应该有所有利润的总和,所有成本的总和,余额=利润-成本

public double getProfitSum()
{
    return db.Profits.Where(p=>p.IdUser.UserId == WebSecurity.CurrentUserId).Sum(p=>p.Value);
}        

public double getCostSum()
{
    return db.Costs.Where(c=>c.IdUser.UserId == WebSecurity.CurrentUserId).Sum(c=>c.Value);
}

控制器:

 public ActionResult Index()
 {


      var pcv = new ProfitAndCostViewModel();
      pcv.ProfModel = getProfitSum();  //This should be a double
      pcv.CostModel =getCostSum();   //This should be a double
      return View(pcv);       
 }

模型:

public class ProfitAndCostViewModel
{
    public double ProfModel {get;set;}
    public double CostModel {get;set;}
    public FinalBalance {get{ return ProfModel - CostModel;} }
}

看法:

@model WHFM.ViewModels.ProfitAndCostViewModel
@Model.FinalBalance - This show now be correct
于 2013-03-15T14:33:18.860 回答
0
public IEnumerable<ProfitAndCostViewModel> getTotalBalance()
{
    var total = from p in db.Profits
                join c in db.Costs on p.IdUser.UserId equals c.IdUser.UserId
                group new { p, c } by p.IdUser.UserId into g
                select new ProfitAndCostViewModel
                {
                    FinalBalance = g.Sum(i => i.p.Value) - g.Sum(i => i.c.Value);
                };
    return total;        
}
于 2013-03-15T11:33:52.457 回答
0

尝试

 public IEnumerable<ProfitAndCostViewModel> getTotalBalance()
 {
     var total = from p in db.Profits
                 join c in db.Costs on p.IdUser.UserId equals c.IdUser.UserId
                 where p.IdUser.UserId == WebSecurity.CurrentUserId
                 group new { p, c } by p.IdUser.UserId into g
                 select new ProfitAndCostViewModel
                 {
                    FinalBalance = g.Sum(i => i.p.Value) - g.Sum(i => i.c.Value)
                 };
     return total;       
  } 
于 2013-03-15T12:07:49.570 回答