sqlkata/querybuilder

Updated example for Includes

Reddevildragg opened this issue · 5 comments

Quick question around is there an updated example for the Includes function?

The documentation shows the function taking 1 parameter (which i assume is a query for the table you want to connect into

var books = db.Query("Books")
    .Include(db.Query("Authors")) // Assumes that the Books table have a `AuthorId` column
    .Get()

However version 2.3.7 the include function appears to take a minimum of 2 parameters for relationName and query, but also optional for foreignkey, localKey and is many.

Is there an example of how to pull this together under the new parameters.

What i have tried to far is as followed:

db.Query("User").Where("User.Id", index).Include("Project",db.Query("Projects"), "ProjectId", "Id", false).Get<User>();

Which runs but then when running the "Project" field within the User Class remains as Null, not sure if i have the request wrong or if the mapping is into a class is not working as i thought it may be. For this case my User Class has the following:

public int ProjectId { get; set; }
public Project Project { get; set; }

Project Id gets mapped, but not project

Thanks for any help.

So upon playing around a bit it looks like the above is the correct way to write the include.

However it only works if i dont pass a type into the Get function to cast it from anything other than a dynamic. Looking into the code because my class does not inhearit both IDynamicMetaObjectProvider and IDictionary<string, object> the handleIncludes function just returns the results when executing.

Is there a work around for casting with the nested classes or is this currently a limitation of the system, i can work around with if so, but just checking im not missing anything ovbious on how i have set up the systems

No unfortunately the include functionality is limited by the Dynamic types for now, I didn't find an easy way to achieve that for non dynamic types at the moment.

No worries, I can work around it with a couple of requests, was just wondering if i had missed anything ovbious.

If i get time may have a play around with the source code and see if can think of an elegant solution, but cant promise anything :)

Thanks for the quick response