如何使用linq减去两列的总和

时间:2014-07-15 07:31:13

标签: linq

在我的查询中,如果没有正常工作可以帮助吗?

enter code here 
       var collectionamount = (from u in db.bank_deposit
                                    join pr in db.collections on u.AgentID equals pr.AgentID into g
                                    join agentname in db.agent_master on u.AgentID equals agentname.AgentID
                                    group u by new { u.AgentID } into x
                                    select new collectionwithdeposit
                                    {
                                        AgentName = agentname.AgentName,
                                        DepositedAmount = g.Sum(m => m.DepositedAmount),
                                        CollectionAmount = g.Sum(z => z.AmountReceived),
                                        Balance = g.Sum(u => u.DepositedAmount) - g.Sum(v => v.AmountReceived)
                                    }).ToList();

            lstdataModel.Add(dataModel);

        }

1 个答案:

答案 0 :(得分:1)

g范围内不存在变量select。这是因为into关键字。你写的是有效的..

var tempEnumerable = from u in db.bank_deposit
                     join pr in db.collections on u.AgentID equals pr.AgentID into g
                     join agentname in db.agent_master on u.AgentID equals agentname.AgentID
                     group u by new { u.AgentID };
var collectionamount = from x in tempEnumerable
                                    select new collectionwithdeposit
                                    {
                                        AgentName = agentname.AgentName,
                                        DepositedAmount = g.Sum(m => m.DepositedAmount),
                                        CollectionAmount = g.Sum(z => z.AmountReceived),
                                        Balance = g.Sum(u => u.DepositedAmount) - g.Sum(v => v.AmountReceived)
                                    }).ToList();

Linq中的into关键字用于创建新范围。

事实上,你真的不想像你一样经常使用

试试这个

var collectionAmount = from u in db.bank_deposit
                     where db.collections.Any(pr => pr.AgentID == u.AgentID)
                     join agentname in db.agent_master on u.AgentID equals agentname.AgentID
                     group u by agentname into g
                     select new collectionwithdeposit
                     {
                         AgentName = g.Key.AgentName,
                         DepositedAmount = g.Sum(m => m.DepositedAmount),
                         CollectionAmount = g.Sum(z => z.AmountReceived),
                         Balance = g.Sum(u => u.DepositedAmount) - g.Sum(v => v.AmountReceived)
                     }

编辑:

也许你真正想要的是这个...我不知道你的桌子结构。但我会假设你对Linq一无所知,而且你的表格是理智的(因为我的上一个答案是相反的)。

var collectionAmount = from agent in db.agent_master
                       let depositedAmount = db.bank_deposit
                               .Where(d => d.AgentID == agent.AgentID)
                               .Sum(d => c.DepositedAmount)
                       let collectionAmount = db.collections
                               .Where(c => c.AgentID == agent.AgentID)
                               .Sum(c => c.AmountReceived)
                       select new collectionwithdeposit
                       {
                         AgentName = agent.AgentName,
                         DepositedAmount = depositedAmount,
                         CollectionAmount = collectionAmount,
                         Balance = depositedAmount - collectionAmount
                       }