querybuilder icon indicating copy to clipboard operation
querybuilder copied to clipboard

How to use join with like automapping?

Open robbelroot opened this issue 3 years ago • 4 comments

When i use a join (which works as i can see when i look at the non generic function return value) with the generic overload GetAsync<Entity>(), then the relational properties are still null. Am i missing something or isn't it possible with sqlkata?

robbelroot avatar Nov 01 '20 16:11 robbelroot

@robbelroot could you provide a code example?

ahmad-moussawi avatar Nov 02 '20 08:11 ahmad-moussawi

Sure, but it's just a trivial join, nothing special: _db.Query($"Jobs") .Join($"Customers", "Jobs.customer_id", "Customers..id")

And the Job Class: public class Job : Entity<long> { public Customer Customer { get; set; } public Job() { } }

robbelroot avatar Nov 03 '20 14:11 robbelroot

No, it doesn't work this way, the joined columns will be returned on the same level as the original table record. For example, the result class should be similar to:

public class JobWithCustomer {
 public int JobId;
 public string Position;
 public int CustomerId;
 public string CustomerName;
}

for a query like this one

db.Query("Jobs").Join("Customers", ...).Select("Jobs.{JobId, Position}", "Customers.{CustomerId, CustomerName}");

You can use the .Include and .IncludeMany to include related properties, but the only limitation, for now, is that this will work only on the non-typed overload of Get and GetAsync.

ahmad-moussawi avatar Nov 04 '20 13:11 ahmad-moussawi

_db.Query("job").Include("customer", q => q.From("customer"))

Gives me the error:

Lambda expression cannot be converted to type Query because it's no delegate type.

Got version 2.2.0 of your Package.

EDIT: I just saw that the current version seems to use just a Query as second parameter, so i changed q => q.Query("customer") to just _db.Query("customer")

robbelroot avatar Nov 07 '20 13:11 robbelroot