SelectMany 内的 Null 条件运算符 - 仍然收到 NullReferenceException
Null Conditional operator inside SelectMany - still getting NullReferenceException
接受的答案@rob
我需要 .Where()
Clause/Operator
@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 不同,但值得一试)
- 添加另一个 Null 条件到 .Select(jpc => jpc?.ChildID)??Guid.Empty
这是代码的其余部分:
(顺便说一句:我完全接受其他更好的方法来通过 DAOParent class 进行初始化;但绝对想在这种情况下学习正确的空条件语法)
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...
}
}
当它为空时,您的查询将最终成为 .SelectMany(p => null)
,这可能 不是 您想要的。您应该在到达 SelectMany
.
之前过滤集合
此外,ToList()
永远不会 return null
,因此您无需提供默认值。例如:
List<Guid> ExistingChildrenIDsFromParentsIcollectionJoinParentChildM2M =
this.parent
.Where(p => p.joinParentChildM2M != null)
.SelectMany(p => p.joinParentChildM2M.Select(jpc => jpc.ChildID))
.ToList();
接受的答案@rob
我需要 .Where()
Clause/Operator
@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 不同,但值得一试)
- 添加另一个 Null 条件到 .Select(jpc => jpc?.ChildID)??Guid.Empty
这是代码的其余部分:
(顺便说一句:我完全接受其他更好的方法来通过 DAOParent class 进行初始化;但绝对想在这种情况下学习正确的空条件语法)
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...
}
}
当它为空时,您的查询将最终成为 .SelectMany(p => null)
,这可能 不是 您想要的。您应该在到达 SelectMany
.
此外,ToList()
永远不会 return null
,因此您无需提供默认值。例如:
List<Guid> ExistingChildrenIDsFromParentsIcollectionJoinParentChildM2M =
this.parent
.Where(p => p.joinParentChildM2M != null)
.SelectMany(p => p.joinParentChildM2M.Select(jpc => jpc.ChildID))
.ToList();