Search code examples
c#.netsql-serverllblgenpro

Nested Select in LLBLGen


So, I have the following tables:

Tables

Using LLBLGen 2.6 (Adapter Version - No Linq), SQL Server, and .NET 3.5, how would I write the following query?

SELECT o.ObjectID 
FROM Object o 
INNER JOIN ObjectDetail d ON i.ObjectID = d.ObjectID 
WHERE d.CreatedDate = ( SELECT MAX(CreatedDate) 
                        FROM ObjectDetail
                        WHERE ObjectID = o.ObjectID
                      )

There will be more filtering, however it's not relevant to this, like if I had an ObjectDetailType and I wanted the max ObjectDetail row for a certain type.

Also, it doesn't have to select o.ObjectID, any / all columns will be fine.


Solution

  • Solved it

    PredicateExpression.AddWithAnd(
      new FieldCompareSetPredicate(
        ObjectDetailFields.CreatedDate,
        null,
        ObjectDetailFields.CreatedDate.SetAggregateFunction(AggregateFunction.Max),
        null,
        SetOperator.Equal,
        (ObjectFields.ObjectID == ObjectDetailsFields.ObjectID)
     )
    );