LINQ how to return last date and difference between first and last count

寵の児 提交于 2021-02-10 06:16:25

问题


I have a table with the following column and sample data:

acteename   updated_at              count (count is not sorted in db)

dev-52      2/7/2020 5:56:43 PM     1
dev-52      2/7/2020 5:56:52 PM     2
dev-52      2/7/2020 5:57:00 PM     3
dev-52      2/7/2020 5:57:49 PM     4
dev-52      2/7/2020 5:59:19 PM     5
dev-52      2/7/2020 6:01:51 PM     6
dev-52      2/7/2020 6:06:21 PM     7
dev-52      2/7/2020 6:14:51 PM     8
dev-52      2/7/2020 6:31:23 PM     9
dev-52      2/7/2020 6:47:54 PM     10
dev-52      2/7/2020 7:04:26 PM     11
dev-52      2/7/2020 7:20:58 PM     12

I want to return diff between first and last count sorted by updated_at and last updated_at where acteename is equal to some name that I get from different table

IQueryable<Events> list = _DBcontext.Events.GroupBy(p => p.ActeeName)
                            .Select(g => new
                            {
                                acteename = g.Key,
                                lastupdated = g.OrderByDescending(p => p.UpdatedAt).First(),
                                firstcount = g.OrderBy(p => p.UpdatedAt).First().Count,
                                lastcount = g.OrderByDescending(p => p.UpdatedAt).First().Count
                            })
                            .Select(x => new { x.acteename,x.lastupdated, diff = x.lastcount - x.firstcount })
                            .Where(a => a.acteename == item.AppName)
                            .ToList();

Getting following Error:

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: string acteename, TestApp.Models.Events lastupdated, int diff>>' to 'System.Linq.IQueryable<TestApp.Models.Events>'. An explicit conversion exists (are you missing a cast?)

expected output:

dev-52      2/7/2020 7:20:58 PM     11

kinda stuck on this not sure if the LINQ query is correct or not, any help will be useful :)

Update:

Exact Query that I ran:

var list = from p in _DBcontext.AppProcessCrashEvents
                                   where p.Actee == item.AppGuid
                                   group p by p.Actee
            into gList
                                   select new
                                   {
                                       acteenamex = gList.Key,
                                       lastUpdated_at = gList.OrderBy(x => x.UpdatedAt).Last().UpdatedAt,
                                       diff = gList.OrderBy(x => x.UpdatedAt).Last().CrashCount -
                                                             gList.OrderBy(x => x.UpdatedAt).First().CrashCount
                                   };

回答1:


One can apply the filter in very beginning itself. Use group by with into and then finally select what you need. Since its creating a anonymous object hence it would be better to start with 'var`.

Edited: Fetch the data in memory and then apply group by to avoid any problem in mapping with database side grouping.

var list =  (from p in _DBcontext.Events
            where p.acteename == item.AppName select p).ToList();

var fList = (from p in list 
            group p by p.acteename
            into gList
            select new
            {
               acteenamex = gList.Key,
               lastUpdated_at = gList.OrderBy(x => x.updated_at).Last().updated_at,
               diff = gList.OrderBy(x => x.updated_at).Last().count - 
                                      gList.OrderBy(x => x.updated_at).First().count
            }).ToList();



回答2:


You can filter the list by the AppGuid first, then order it, and then convert that to list. Now, use this list in a new variable to get the records you need from it.

var gList = _DBcontext.AppProcessCrashEvents
        .Where(x => x.Actee == item.AppGuid)
        .OrderByDescending(p => p.UpdatedAt)
        .ToList();

var result = new Events
{
    Actee = gList[0].Actee,
    UpdatedAt = gList[0].UpdatedAt,
    CrashCount = gList[0].CrashCount - gList[gList.Count - 1].CrashCount
};

I hope this is what are you looking for.



来源:https://stackoverflow.com/questions/60141102/linq-how-to-return-last-date-and-difference-between-first-and-last-count

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!