gpt4 book ai didi

c# - LINQ 如何返回最后日期以及第一次和最后一次计数之间的差异

转载 作者:行者123 更新时间:2023-12-04 10:36:47 24 4
gpt4 key购买 nike

我有一个包含以下列和示例数据的表:

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

我想返回 first 之间的差异和 lastupdated_at 排序的计数最后 updated_at where acteename等于我从不同表中获得的某个名称
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();

得到以下错误:

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?)



预期输出:
dev-52      2/7/2020 7:20:58 PM     11

有点卡在这不确定 LINQ 查询是否正确,任何帮助都会有用:)

更新:

我运行的确切查询:
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
};

最佳答案

人们可以在一开始就应用过滤器本身。使用 group byinto然后最后选择你需要的。由于它创建了一个匿名对象,因此最好以“var”开头。

已编辑:获取内存中的数据然后应用 group by避免在与数据库端分组映射时出现任何问题。

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();

关于c# - LINQ 如何返回最后日期以及第一次和最后一次计数之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60141102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com