小编str*_*ner的帖子

SelectMany 中的 Null 条件运算符 - 仍然得到 NullReferenceException

接受的答案@rob

我需要一个.Where()条款/运算符

奖励指向@juharr 直接回答误导性问题

List<Guid> ExistingChildrenIDsFromParentsIcollectionJoinParentChildM2M =
        this.parent
        .SelectMany(
            p => p.joinParentChildM2M?
            .Select(jpc => jpc.ChildID)
            ?? new List<Guid> {Guid.Empty} //tried similar, but didn't understand the type needs of .SelectMany
            )
        .ToList();
Run Code Online (Sandbox Code Playgroud)

原问题:

我在洞里大约有 10 个搜索/40 个结果,但在这件事上一无所获……TIA!

该语句抛出 NullReferenceException :
(p.joinParentChildM2M 有时初始化为 null 有时不初始化)

List<Guid> ExistingChildrenIDsFromParentsIcollectionJoinParentChildM2M =
        this.parent
        .SelectMany(
            p => p.joinParentChildM2M? //shouldn't this Null Conditional Operator break the chain?
            .Select(jpc => jpc.ChildID)
            )
        .ToList()
        ??
        GuidEmptyList();
Run Code Online (Sandbox Code Playgroud)

我试过:
- 移动 Coalesce ?? 在 SelectMany Parens() 中
- 添加 DefaultIfEmpty(意识到空与 …

c# linq nullreferenceexception c#-6.0

-1
推荐指数
1
解决办法
2216
查看次数

标签 统计

c# ×1

c#-6.0 ×1

linq ×1

nullreferenceexception ×1