SelectMany中的Null Conditional运算符 - 仍然获得NullReferenceException

时间:2017-01-26 23:43:37

标签: 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();

原始问题:

我大约有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();

我试过了:
  - 移动Coalesce?在SelectMany Parens()里面   - 添加DefaultIfEmpty(实现空不同于Null,但值得一试)
  - 在.Select(jpc => jpc?.ChildID)??Guid.Empty

中添加另一个Null条件

以下是代码的其余部分:
(顺便说一句:我完全接受其他更好的方法来通过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...
        }
    }

1 个答案:

答案 0 :(得分:4)

当它为空时,您的查询将最终为.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();