querybuilder icon indicating copy to clipboard operation
querybuilder copied to clipboard

Updated example for Includes

Open Reddevildragg opened this issue 2 years ago • 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.

Reddevildragg avatar Apr 06 '22 17:04 Reddevildragg

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

Reddevildragg avatar Apr 06 '22 18:04 Reddevildragg

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.

ahmad-moussawi avatar Apr 06 '22 18:04 ahmad-moussawi

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

Reddevildragg avatar Apr 07 '22 08:04 Reddevildragg

@Reddevildragg would you mind sharing an example please? A few of us here https://github.com/sqlkata/querybuilder/issues/308 are struggling to find an efficient way to cast it when it seems buried in dictionaries.

brioni78 avatar Nov 14 '23 17:11 brioni78

@Reddevildragg would you mind sharing an example please? A few of us here #308 are struggling to find an efficient way to cast it when it seems buried in dictionaries.

Yes, please share an example, haven't solved it yet

trishmun avatar Feb 01 '24 17:02 trishmun