LiNQ连接两个表用于减法SUM

时间:2013-03-15 11:30:18

标签: linq

场景:View应该包含所有利润的总和,所有成本的总和以及余额=利润 - 成本

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 个答案:

答案 0 :(得分: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};
}

从我所看到的,你只是在寻找当前用户的最终余额,对吧?如果是这样,您不需要返回IEnumerable的ProfitAndCostViewModel。

如果你想拥有多对多的用户平衡输出(IEnumerable of ProfitAndCostViewModel),那么试试:

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

我只在列表上测试过这个,而不是数据库变量,所以它可能无效。

答案 1 :(得分:1)

试试这个:

场景:View应该包含所有利润的总和,所有成本的总和以及余额=利润 - 成本

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

答案 2 :(得分: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;        
}

答案 3 :(得分: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;       
  }