It's possible to do this. You have to create a DerivedTableDefinition for the 'A' table (which is a derived table). Then, you do the following:
- Create a DynamicRelation by using the constructor which accepts a DerivedTableDefinition. You have specified as alias for the derived table definition 'A'.
- Create a RelationCollection, and add the created dynamic relation to that relationcollection.
- Create a ResultsetFields object add two fields to it, which both have their ObjectAlias set to "A".
- Create a groupbycollection and SortExpression and both use the first field in the resultsetfields.
- Pass it all to the dynamiclist fetch method to fetch the query.
With Linq this is much easier (and doable all in the db), but I see you use .NET 2.0, so you've to use our native query system
I've posted below a tryout on Northwind which produces a similar query. (I didn't include the is null filter, but that's trivial)
// derived table elements. Define a derived query where all orderdetails rows with productid1 are fetched and the count of the other products
// in that query.
int productId = 1;
PredicateExpression scalarFilter = new PredicateExpression();
scalarFilter.Add(OrderDetailFields.ProductId.SetObjectAlias("ODI") != productId);
scalarFilter.AddWithAnd(OrderDetailFields.OrderId.SetObjectAlias("ODI")
== OrderDetailFields.OrderId);
ResultsetFields dtFields = new ResultsetFields(2);
dtFields.DefineField(OrderDetailFields.OrderId, 0);
dtFields.DefineField(new EntityField("OtherProductsInGroup",
new ScalarQueryExpression(OrderDetailFields.OrderId
.SetAggregateFunction(AggregateFunction.Count).SetObjectAlias("ODI"),
scalarFilter)
), 1);
PredicateExpression dtFilter = new PredicateExpression();
dtFilter.AddWithAnd(OrderDetailFields.ProductId == productId);
DerivedTableDefinition dt = new DerivedTableDefinition(dtFields, "A", dtFilter);
dt.AllowDuplicates = false;
RelationCollection relations = new RelationCollection(new DynamicRelation(dt));
ResultsetFields fields = new ResultsetFields(2);
fields.DefineField(new EntityField("OtherProductsInGroup", "A", typeof(int)), 0);
fields.DefineField(new EntityField("OrderId", "A", typeof(int)).SetAggregateFunction(AggregateFunction.Count), 1);
GroupByCollection groupBy = new GroupByCollection(fields[0]);
SortExpression sorter = new SortExpression((EntityField)fields[0] | SortOperator.Ascending);
DataTable results = new DataTable();
TypedListDAO dao = new TypedListDAO();
dao.GetMultiAsDataTable(fields, results, 0, sorter, null, relations, false, groupBy, null, 0, 0);
SQL:
SELECT DISTINCT [LPA_A1].[OtherProductsInGroup], COUNT([LPA_A1].[OrderId]) AS [OrderId]
FROM
(
SELECT DISTINCT [Northwind].[dbo].[Order Details].[OrderID] AS [OrderId],
(
SELECT COUNT([ODI].[OrderID]) AS [OrderId]
FROM [Northwind].[dbo].[Order Details] [ODI]
WHERE ( [ODI].[ProductID] <> @ProductId1
AND [ODI].[OrderID] = [Northwind].[dbo].[Order Details].[OrderID])
) AS [OtherProductsInGroup]
FROM [Northwind].[dbo].[Order Details]
WHERE ( [Northwind].[dbo].[Order Details].[ProductID] = @ProductId2)
) [LPA_A1]
GROUP BY [LPA_A1].[OtherProductsInGroup]
ORDER BY [LPA_A1].[OtherProductsInGroup] ASC