如何在OrderBy中使用Linq查询结果?

时间:2014-07-18 09:39:33

标签: c# linq

这里我有一个方案来获取本月(当月)日期的数据

Excepted Result
     Date_time        sum(collection.amountreceived )  Sum(bank_deposit.depositamount)
       1/07/2014                 2000                 1000
       2/07/2014                 3000                 3000

模式

bank_deposit

   agentid         (nvarchar(30))   
   depositamount   (DECIMAL(10,0))    
   date_time       (TIMESTAMP)

collection

   customeridn       (varchar(30))
   amountreceived    (DECIMAL(10,0))
   date_time         (TIMESTAMP)  
   agentid           (nvarchar(30))

这里我使用union来获取一列中的datetime列数据

 var unionDateColumn = ((from agent in db.collections select agent.Date_Time)
        .Union(from u in db.bank_deposit select u.Date_Time)).ToList();

如何将此unionDateColumn数据用于orderby并获得预期的输出?

以下是对金额总和的查询,但这里我的问题是如何

var model = (from coll in db.collections.Where(e => e.AgentID == item.AgentID)
                          let depositedAmount = db.bank_deposit.Where(d => d.AgentID == item.AgentID ).Sum(c => c.DepositedAmount) == null ? 0
                          : db.bank_deposit.Where(d => d.AgentID == item.AgentID).Sum(x => x.DepositedAmount)
                          let collectionAmount = db.collections.Where(c => c.AgentID == item.AgentID).Sum(c => c.AmountReceived) == null ? 0
                          : db.collections.Where(v => v.AgentID == item.AgentID).Sum(m => m.AmountReceived)
                          select new GetBalanceAmount
                          {
                              DepositedAmount = depositedAmount,
                                 CollectionAmount = collectionAmount
                          });

2 个答案:

答案 0 :(得分:1)

我假设你想按date_time

订购结果
var result = unoinDateColumn.OrderBy(t=>t.Date_Time).ToList().;

答案 1 :(得分:0)

为何选择FirstOrDefault?我想你想要每一行。

Sajeetharan的例子: var result = unoinDateColumn.OrderBy(t => t.Date_Time).ToList(); 结果与"选择* from unionDateColumn order by DateTime"

你的例子: 。model.ToList()OrderByDescending(unoinDateColumn).FirstOrDefault();

与"从模型订单中选择*由unionDateColumn" 请问你为什么要分别处理每一栏?在一个大阵列中插入?

在一个数组中处理它,并像这个模型一样.OrderByDescending(x => x.Date_Time);

它会影响对象模型并按照此模型对其进行排序= model.sortedBy(model.Date_Time);