I'm trying to get the following SQL to work with llblgen...
SELECT *,(SELECT TOP (1) Id FROM Content.Grades WHERE Account = Authentication.Account.Id ORDER BY Grades.GradingDate DESC) AS CurrentGrade FROM Authentication.Account WHERE (SELECT TOP (1) Grade FROM Content.Grades WHERE Account = Authentication.Account.Id ORDER BY Grades.GradingDate DESC) = 5
var dtFields = new ResultsetFields(1);
dtFields.DefineField(GradesFields.Id, 0);
var dtDefinition = new DerivedTableDefinition(
dtFields, "c", new PredicateExpression(GradesFields.Grade == SelectedGrade.Value));
// specify the relation which is a dynamic relation.
var relation = new DynamicRelation(dtDefinition, JoinHint.Inner,
EntityType.GradesEntity, "o",
(new EntityField2(AccountFields.Id.ToString(), "c", typeof(string)) ==
GradesFields.Account.SetObjectAlias("o")));
RelationBucket.Relations.Add(relation);
I'm struggling to get this to fit as everytime I try the query i'm getting errors on the field...
Ok I solved my own issue here.
When using a derived table we need to set an object alias on all references. This include the sort fields and predicate fields.
var dtFields = new ResultsetFields(2);
dtFields.DefineField(GradesFields.Account, 0);
dtFields.DefineField(GradesFields.Grade, 1);
var dtDefinition = new DerivedTableDefinition(
dtFields, "c", null, null,
new SortExpression(GradesFields.GradingDate | SortOperator.Descending), null, 1, false);
// specify the relation which is a dynamic relation.
var relation = new DynamicRelation(dtDefinition, JoinHint.Inner,
EntityType.AccountEntity, "o", (AccountFields.Id.SetObjectAlias("o") ==
GradesFields.Account.SetObjectAlias("c")));
repositoryQuery.Relations.Add(relation);
repositoryQuery.PredicateExpression.Add(new EntityField2("Grade", "c", typeof(int)) == SelectedGrade.Value);
AccountFields.Lastname.SetObjectAlias("o") | SortOperator.Ascending,