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 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 に答える
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;
}

これは DB 変数ではなくリストでのみテストしたため、機能しない可能性があります。

于 2013-03-15T13:31:17.103 に答える
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 に答える