0
 var abc1 = from dlist in db.DebtorTransactions.ToList()
                       join war in db.Warranties on dlist.ProductID equals war.Id
                       join ag in db.Agents on war.fldAgentID equals ag.pkfAgentID
                       join sr in db.SalesReps on war.fldSrId equals sr.pkfSrID
                       where dlist.TransTypeID == 1
                       select new
                       {
                          dlist.Amount,
                           dlist.TransTypeID,
                           name = ag.Name,
                           ag.pkfAgentID,
                           sr.pkfSrID,
                           salesnam = sr.Name
                       } into objabc
                       group objabc by new
                       {
                           objabc.TransTypeID,
                           objabc.name,
                           objabc.salesnam,
                           objabc.Amount
                       };
            var amt1 = abc1.Sum(x => x.Key.Amount);


            var abc2 = from dlist in db.DebtorTransactions.ToList()
                       join cjt in db.CarJackaTrackas on dlist.ProductID equals cjt.pkfCjtID
                       join ag in db.Agents on cjt.AgentID equals ag.pkfAgentID
                       join sr in db.SalesReps on cjt.SalesRepId equals sr.pkfSrID
                       where dlist.TransTypeID == 0
                       select new
                       {
                           dlist.Amount,
                           dlist.TransTypeID,
                           name = ag.Name,
                           ag.pkfAgentID,
                           sr.pkfSrID,
                    enter code here`       salesnam = sr.Name
                       } into objabc
                       group objabc by new
                       {
                           objabc.TransTypeID,
                           objabc.name,
                           objabc.salesnam,
                           objabc.Amount
                       };
            var amt2 = abc1.Sum(x => x.Key.Amount);
            //var result1=
            return View();

我是 linq 的新手,这个查询正在工作,但我需要通过单个查询获得 dlist.TransTypeID == 0 和 dlist.TransTypeID == 1 的金额之和。有人可以帮助我吗?提前致谢

4

1 回答 1

0

这是一个精简的示例,说明如何做到这一点。如果需要,您可以添加连接,但我不清楚为什么需要一些额外的连接值。

var transTypeAmountSums = (from dlist in db.DebtorTransactions
                           group dlist by dlist.TransTypeId into g
                           where g.Key == 0 || g.Key == 1
                           select new
                           {
                               TransTypeId = g.Key,
                               AmountSum = g.Sum(d => d.Amount)
                           }).ToDictionary(k => k.TransTypeId, v => v.AmountSum);

int transTypeZeroSum = transTypeAmountSums[0];
int transTypeOneSum = transTypeAmountSums[1];

有几点需要注意:

  1. 我删除了ToList(). 除非您想将所有 DebtorTransactions 放入内存然后对这些结果运行 Linq 操作,否则您将希望将其排除在外,让 SQL 来处理聚合(它比 C# 好得多)。
  2. 我只分组dlist.TransTypeId。如果需要,您仍然可以按更多字段进行分组,但是在示例中不清楚为什么需要它们,所以我只是做了一个简化的示例。
于 2013-10-16T13:00:00.290 回答