gpt4 book ai didi

c# - Linq C# 基于最大值

转载 作者:太空宇宙 更新时间:2023-11-03 18:04:12 25 4
gpt4 key购买 nike

我从数据库中获取数据,结果如下结构。按InputFileIdFilename分组,只选择Failed的最大值。

InputFileId = 12, FileName = "Filename_12", Failed = false
InputFileId = 13, FileName = "Filename_13", Failed = false
InputFileId = 13, FileName = "Filename_13", Failed = true

现在我只想看到这个结果:

InputFileId = 12, FileName = "Filename_12", Failed = false
InputFileId = 13, FileName = "Filename_13", Failed = true

到目前为止,我的 Linq 查询是:

Files = (this.InputFileStatusService.GetFilesUploadedByDay(StartOfDay(SelectedDate), EndOfDay(SelectedDate), this.Deal.Id))
.Where(a => a.InputFileId > 0)
.GroupBy(m => new { m.InputFileId, m.FileName, Failed = m.Failed ? 1 : 0 })
.Select(m => new
{
InputFileId = m.Key.InputFileId,
FileName = m.Key.FileName,
Failed = m.Max(x=>x.Failed)
}).ToList();

但这不起作用,有人对此有解决方案吗?

最佳答案

应该足以将 Max 替换为 Any

Failed = m.Any(x => x.Failed) 

并删除 Failed = m.Failed ? 1 : 0

.GroupBy(m => new { m.InputFileId, Failed = m.Failed ? 1 : 0 })

由于这些列似乎彼此对应,您可能会考虑为 m.FileName 添加一个分组。

关于c# - Linq C# 基于最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39165653/

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