简体   繁体   English

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

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

请检查表设计

I tried to divided Linq queries into 3 (total, success, fail) but so far "Total" Linq query is working fine.我试图将 Linq 查询分为 3 个(总计、成功、失败),但到目前为止“总计”Linq 查询工作正常。 Please help me to get "Success", "Fail" columns (it has mulitple statuses and we have to check the last column of each transaction and destination) Note: you need to group by ProcessTime, TransactionId, Destination and check last column whether it is success or Fail then apply count (we are using oracle as backend)请帮我获取“成功”、“失败”列(它有多种状态,我们必须检查每个事务的最后一列和目的地) 注意:您需要按 ProcessTime、TransactionId、Destination 分组并检查最后一列是否是成功还是失败然后应用计数(我们使用 oracle 作为后端)

LINQ for Total count总计数的 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);

Right now I'm thinking there isn't a need to make it too complex;现在我认为没有必要让它太复杂; I wouldn't bother wrangling another table into a join and then looking at the end of a transaction ID to find a word "Success" or "Fail" on the DB side of things (especially since they have typos).我不会费心将另一个表加入连接,然后查看事务 ID 的末尾以在事物的 DB 端找到“成功”或“失败”一词(尤其是因为它们有拼写错误)。 All your status IDs are nicely even for successes and odd for fails but I'd download them all, grouped:您所有的状态 ID 都很好,即使成功也很奇怪,但我会全部下载它们,分组:

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),
        STAT = route.FILE_STATUS_ID < 7 ? route.FILE_STATUS_ID % 2 : 2
      }
  ).GroupBy(p => p) 
  .ToDictionary(g => g.Key, g => g.Count());

You'll end up with STAT of 0 for Success, 1 for Failed or 2 for Completed您最终的 STAT 为 0 表示成功,1 表示失败或 2 表示已完成

If you want the total, just Sum() locally如果你想要总数,只需在本地 Sum()

Check this solution.检查此解决方案。 If it gives wrong result, then I need more details.如果它给出错误的结果,那么我需要更多的细节。

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.Select(route => new { route.FILE_TRANID, route.DESTINATION }).Distinct()
    from route in routesQuery
        .Where(route => d.FILE_TRANID == route.FILE_TRANID && d.DESTINATION == route.DESTINATION)
        .OrderByDescending(route => route.ROUTE_ID)
        .Take(1)
    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();   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM