LINQ - SELECT最小值,仅当集合不为空时
本文关键字:集合 SELECT 最小值 LINQ | 更新日期: 2023-09-27 18:13:22
我有以下(简化)模型:
public class ClockinReport_PerMachine
{
public ObservableCollection<Clockin_Categories> Categories { get; set; }
}
public class Clockin_Categories
{
public ObservableCollection<ClockinReport_Day> Report { get; set; }
}
public class ClockinReport_Day
{
public DateTime Day { get; set; }
}
然后在我的ViewModel我有以下内容:
public ObservableCollection<ClockinReport_PerMachine> MachineReport {get;set;}
逻辑如下:
- 您有多台机器
- 每台机器有几个类别我们报告(例如错误-无纸,错误-无墨水,…举个例子)
- 在这些类别下,每天都有事件,除了其他事情,我们跟踪问题发生时的确切日期时间
我有以下脚本,它从所有MachineReports
返回最小日期时间PeriodFrom = MachineReport.Min(n => n.Categories.Min(x => x.Report.Min(z => z.Day)));
这个LINQ循环遍历所有机器,循环遍历所有类别,查看报告并检查最小DateTime是什么(至少我认为它是这样做的)。
当Categories不包含任何元素时,出现问题。然后它返回一个错误,说Sequence contains no elements
.
我如何改变我的LINQ,使它忽略类别,如果没有元素?
最后:最后检查集合是否为null对我不起作用,因为我的集合包含0个元素(它在启动时初始化)。因此,我不得不稍微修改一下我的回答:
PeriodFrom = MachineReport.Min(n => n.Categories.Where(b=>b.Report.Count>0).Min(x => x.Report.Max(z => z.Day)));
PeriodFrom = MachineReport.Min(n => n.Categories.
Where(c => c != null).
Min(x => x.Report.Min(z => z.Day)));
由于您选择的是最小值的最小值,您可以使用DefaultIfEmpty()
方法,如下所示:
PeriodFrom = MachineReport
.SelectMany(n =>
n.Categories.SelectMany(x =>
x.Report.SelectMany(z =>
(DateTime?)z.Day
)
)
)
.DefaultIfEmpty()
.Min();
注意当序列为空时,Min()
将给您null
。
您应该测试Categories在表达式中是否不为空。就像
PeriodFrom = MachineReport.Min(n => (n.Categories != null &&
n.Categories.Min(x => x.Report.Min(z => z.Day))));