为什么此LINQ语句返回null而不是计数= 0的IEnumerable



我有一种应该执行以下操作的方法: - 从Web服务中检索对象列表(按预期工作( - 基于几个标准(不工作(返回这些对象的子集

    private List<AliveDTO> getDeads()
    {
        List<AliveDTO> DTOs = APIRequests.Instance.GetAliveDTOs();
        var deads = DTOs.Where(x =>
            x.watchWindowStartTime.CompareTo(DateTime.Now) < 0 ||
            x.watchWindowEndTime.CompareTo(DateTime.Now) > 0 && 
            DateTime.Now > x.timeReceived.AddMinutes((double)x.NextAliveWithinMinutes));
        List<AliveDTO> deadInList = deads as List<AliveDTO>;
        return deadInList;
    }

我期望变量死亡是一个可iNumerable,如果列表中没有实体匹配标准,则count = 0,但是我得到了无效的值。

我做错了什么?

我认为问题是'死者'不会是列表。这将是一个 iEnumerable 。添加.tolist((linq命令将使"死亡"成为列表的类型。我还会稍微重构,因为您正在为死者创建第二个列表。:)

private List<AliveDTO> getDeads()
{
    List<AliveDTO> DTOs = APIRequests.Instance.GetAliveDTOs();
    return DTOs.Where(x =>
        x.watchWindowStartTime.CompareTo(DateTime.Now) < 0 ||
        x.watchWindowEndTime.CompareTo(DateTime.Now) > 0 && 
        DateTime.Now > x.timeReceived.AddMinutes((double)x.NextAliveWithinMinutes)).ToList();
}

最新更新