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

str*_*ner -1 c# linq nullreferenceexception c#-6.0

接受的答案@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(意识到空与 Null 不同,但值得一试)
- 添加另一个 Null Conditional.Select(jpc => jpc?.ChildID)??Guid.Empty

这是代码的其余部分:(
顺便说一句:我完全接受其他更好的方法来初始化 DAOParent 类;但绝对想在这种情况下学习正确的 Null 条件语法)

public class Parent
    {
    public Guid parentID {get; set;}
    //Other Properties...

    //one-way NavigationProperties
    public ICollection<JoinParentChildM2M> joinParentChildM2M { get; set; }
    }

public class JoinParentChildM2M
    {
    public Guid JoinID {get; set;}
    public Guid ParentID {get; set;}
    public Guid ChildID {get; set;}
    }

public class Child
    {
    public Guid childID {get; set;}
    //Other class Properties...

    //one-way NavigationProperties
    public ICollection<JoinParentChildM2M> joinParentChildM2M { get; set; }

    }


public class DAOParent
    {
    private dbContext _db;
    public IList<Parent> parents {get; set;}
    public IList<Child> children {get; set;}
    //Note: there is no IList<JoinParentChildM2M>, but parents contains an ICOllection<JoinParentChildM2M>
    //Other class Properties...

    public DAOParent( dbContext db , ParentIDList ParentIDList)
        {
        //set this._db, etc
        // this.parents will initialize null
        LoadAllChildrenOfParents()
        }

    public DAOParent( dbContext db , ParentIDList ParentIDList, DAOParent existingParents)
        {
        //set this.dbcontext, set this.parents to existingParents, etc
        // this.parents will initialize as non-empty objects
        LoadAllChildrenOfParents()
        }

    private void LoadAllChildrenOfParents()
        {

        //Before I grab new "Child" entities from the db 
        //I want to exclude existing ones already loaded in my POCO
        List<Guid> ExistingChildrenIDsFromParentsIcollectionJoinParentChildM2M =
                this.parent
                .SelectMany(
                    p => p.joinParentChildM2M? //shouldn't this Null Conditional Operator break the chain?
                    .Select(jpc => jpc.ChildID)
                    )
                .ToList()
                ??
                GuidEmptyList();
        }

    private List<Guid> GuidEmptyList()
        {
        List<Guid> g = new List<Guid> { Guid.Empty };
        return g;
        }


        //More code to finish initializing or updating DAOParent...
        }
    }
Run Code Online (Sandbox Code Playgroud)

Rob*_*Rob 7

当它为空时,您的查询最终.SelectMany(p => null)可能不是您想要的。您应该在进入SelectMany.

此外,ToList()永远不会返回null,因此您无需提供默认值。例如:

List<Guid> ExistingChildrenIDsFromParentsIcollectionJoinParentChildM2M =
    this.parent
    .Where(p => p.joinParentChildM2M != null)
    .SelectMany(p => p.joinParentChildM2M.Select(jpc => jpc.ChildID))
    .ToList();
Run Code Online (Sandbox Code Playgroud)