Hi,
I have a small DB of four tables that looks like this
Segment > Family > Class > Commodity
I've also attached a diagram of the db if anyone is interested.
After reading
this post from Mark's blog, I was really excited that this may actually work, so I tried...
var query = await db.Segment.FindAll(db.Segment.Id == code)
.With(db.Segment.Family.As("families").Class.As("classes"))
.FirstOrDefault();
...but, no luck.
after testing a few variations like...
var query2 = await db.Segment.FindAll(db.Segment.Id == code)
.With(db.Segment.Family.As("families"))
.With(db.Segment.Family.Class.As("classes"))
.FirstOrDefault();
var query3 = await db.Segment.FindAll(db.Segment.Id == code)
.With(db.Segment.Family.As("families"))
.With(db.Family.Class.As("classes"))
.FirstOrDefault();
I've seen several posts with conflicting thoughts on this.
This one where Mark himself suggest using "substrate()" (which I couldn't find any other reference to or get it to work), and
this one
where Mark himself says it just isn't possible. Granted these posts
are both pretty old and almost certainly refer to 0.19 or earlier, but
it was all I could find
My classes are set up to have a nested list of each object...
public class Segment
{
public Segment()
{
families = new List<Family>();
}
public string id { get; set; }
public String description { get; set; }
public int search_id { get; set; }
public IList<Family> families { get; set; }
Family and Class objects are modeled in the same fashion.
Eventually I gave up and went with an old-fashioned loop, to get things loaded correctly. It works but it's not ideal.
var query = await db.Segment.FindAll(db.Segment.Id == code)
.With(db.Segment.Family.As("families"))
.FirstOrDefault();
var segment = (Segment)query;
foreach (var f in segment.families)
{
f.classes = await db.Class.FindAll(db.Class.parent_family_id == f.id)
.With(db.Class.Commodity.As("commodities")).ToList<Class>();
}
It's
not as tidy as the .With() call would be, but my main issue with it is
that it will hit the database for EVERY family in the list. Seems like
there must be a better way!
Does anyone have any thoughts on what I'm doing wrong? Any advice at all would be greatly appreciated. Thanks.