LINQ 查询多个组和最新记录的计数 - Oracle DB

LINQ Query Multiple Group and count of latest record - Oracle DB

我尝试将 Linq 查询分为 3 个(总计、成功、失败),但到目前为止“总计”Linq 查询工作正常。请帮助我获得“成功”、“失败”列(它有多种状态,我们必须检查每笔交易和目的地的最后一列) 注意:您需要按 ProcessTime、TransactionId、Destination 分组并检查最后一列是成功还是失败然后应用计数(我们使用 oracle 作为后端)

总计数的 LINQ

 var query = (from filetrans in context.FILE_TRANSACTION
                             join route in context.FILE_ROUTE on filetrans.FILE_TRANID equals route.FILE_TRANID
                             where 
                                filetrans.PROCESS_STRT_TIME >= fromDateFilter && filetrans.PROCESS_STRT_TIME <= toDateFilter 
                             select new { PROCESS_STRT_TIME = DbFunctions.TruncateTime((DateTime)filetrans.PROCESS_STRT_TIME), filetrans.FILE_TRANID, route.DESTINATION }).
                             GroupBy(p => new { p.PROCESS_STRT_TIME, p.FILE_TRANID, p.DESTINATION });
                var result = query.GroupBy(x => x.Key.PROCESS_STRT_TIME).Select(x => new { x.Key, Count = x.Count() }).ToDictionary(a => a.Key, a => a.Count);

检查这个解决方案。如果给出错误的结果,那么我需要更多详细信息。

var fileTransQuery =
    from filetrans in context.AFRS_FILE_TRANSACTION
    where accountIds.Contains(filetrans.ACNT_ID) &&
      filetrans.PROCESS_STRT_TIME >= fromDateFilter && filetrans.PROCESS_STRT_TIME <= toDateFilter
    select filetrans;

var routesQuery =
    from filetrans in fileTransQuery
    join route in context.AFRS_FILE_ROUTE on filetrans.FILE_TRANID equals route.FILE_TRANID
    select route;

var lastRouteQuery = 
    from d in routesQuery.GroupBy(route => new { route.FILE_TRANID, route.DESTINATION })
        .Select(g => new 
        {
            g.Key.FILE_TRANID, 
            g.Key.DESTINATION,
            ROUTE_ID = g.Max(x => x.ROUTE_ID)
        })
    from route in routesQuery
        .Where(route => d.FILE_TRANID == route.FILE_TRANID && d.DESTINATION == route.DESTINATION && d.ROUTE_ID == route.ROUTE_ID)
    select route;

var recordsQuery =
    from filetrans in fileTransQuery
    join route in lastRouteQuery on filetrans.FILE_TRANID equals route.FILE_TRANID
    select new { filetrans.PROCESS_STRT_TIME, route.CRNT_ROUTE_FILE_STATUS_ID };

var result = recordsQuery
    .GroupBy(p => DbFunctions.TruncateTime((DateTime)p.PROCESS_STRT_TIME))
    .Select(g => new TrendData
    {
        TotalCount = g.Sum(x => x.CRNT_ROUTE_FILE_STATUS_ID != 7 && x.CRNT_ROUTE_FILE_STATUS_ID != 8 ? 1 : 0)
        SucccessCount = g.Sum(x => x.CRNT_ROUTE_FILE_STATUS_ID == 7 ? 1 : 0),
        FailCount = g.Sum(x => failureStatus.Contains(x.CRNT_ROUTE_FILE_STATUS_ID) ? 1 : 0),
        Date = g.Min(x => x.PROCESS_STRT_TIME)
    })
    .OrderBy(x => x.Date)
    .ToList();