We have created a framework that helps us quickly creating code for fetching, sorting, filtering, inserting and updating data consumed by a REST client.
Now we need a way to dynamically join to a table, to get a value from the joined table into a projected field.
Our model:
public class CompanyPotential
{
[AutoLabel]
public long LabelRecordId { get; set; }
}
The DTO projected to the client:
public class CompanyPotentialOutDto
{
[AutoLabel(nameof(CompanyPotentialOutDto.Name))]
public long LabelRecordId { get; set; }
public string Name { get; set; }
}
The model we want to join with (also shortened):
public class ClientLabelValueView
{
public long LabelRecordId { get; set; }
public long LanguageRecordId { get; set; }
public string LabelValue { get; set; }
}
What all this means, is that fields decorated with that AutoLabel attributes, will be handled by the framework. There can be 0 to N labeled fields for every entity in our application.
In the example above, we want to join CompanyPotential with ClientLabelValueView on LabelRecordId, to project ClientLabelValueView.LabelValue into CompanyPotentialOutDto.Name.
Because all this is dynamic, our framework works with expression trees.
What I have tried:
I have successfully injected a join on ClientLabelValueView into the basic query.
What I'm struggling with, is to inject a member initialization in the projection block.
And I also need to inject a criteria for the joined table in the where clause.
All of this can be created through Linq, so it stands to reason that it should be possible to create it using expression trees.
I have tried to use ExpressionVisitor to inject the init fields, and it works great if I use constant values. But when I try to get the value from the joined table, I get this error:
Quote:
The parameter 'MyParameterName' was not bound in the specified LINQ to Entities query expression.
I can't seem to find anything about joins in expression trees on Google, but I hope that one of you guys knows what to do!