First / FirstOrDefault doesn't work as I would expect
I am using nhibernate and I wrote a linq query and it does not return what I expected.
public ParentA()
{
public virtual Id {get; set;}
public virtual Name {get; set;}
public virtual IList<ChildA> ChidrenA {get; set;}
public ParentA()
{
ChidrenA = new List<ChildA>();
}
}
public ChildA()
{
public virtual Id {get; set;}
public virtual Name {get; set;}
public virtual IList<ChildB> ChidrenB {get; set;}
public virtual ParentA ParenteA {get; set;}
public ChildA()
{
ChidrenB = new List<ChildB>();
}
}
public ChildB()
{
public virtual Id {get; set;}
public virtual Name {get; set;}
public virtual ChildA {get; set;}
}
The above code is my domains. Free nhibernate would be very simple and nothing fancy was happening, so I didn't include it.
I have a request
base.unitOfWork.Session.Query<ParentA>()
.Where(x => x.Id == parentAId)
.FetchMany(x => x.ChildrenA)
.ThenFetchMany(x => x.ChildrenB)
.FirstOrDefault();
What i expected to see
It will find 1 or 0 parent entries. If it finds one entry, it will download all ChildrenA files and then all children.
What's happening
It finds 1 or 0 parent records. Then it only accepts 1 or 0 records for ChildrenA and ChildrenB.
Why only the first found record for ChildrenA and ChildrenB?
If I change FirstToDefault()
to .toList()
, I get everything I expect, but I find it pointless as there should only be one entry with this parent entry.
source to share