Sorting on child entities

Posts   
 
    
ts_mjolner
User
Posts: 13
Joined: 28-Sep-2007
# Posted on: 28-Sep-2007 08:27:09   

Hi there!

Let me start out by saying that I have tried searching around on the forum to find the answers, but with no luck simple_smile

I run an application which uses LLBLGen Pro 2.5 using Adaption mode.

My problem is that I would like to create a SortExpression (SortClause) which sorts on a child of the entity being fetched. All the examples I can find does it using SelfServicing and PrefetchPaths, I tried to convert that to Adaption but with no luck, when I look at the SQL the FetchEntityColletion fires there is no sort at all confused

For simplicity let's say I have an SerialEntity which is attached to an ItemEntity. Then I would like to show a list of Serials (for no specific Item, just all in the system) but sorted on the Item which they are attached to.

Now the code I tried (based on the SelfServicing Collection.GetMulti examples I found) is:


Dim sorter As New SortExpression()
sorter.Add(New SortClause(ItemFields.Number, Nothing, SortOperator.Ascending))
pp = New PrefetchPath2(CType(EntityType.ItemEntity, Integer))
pp.Add(ItemEntity.PrefetchPathSerial, 1, Nothing, Nothing, sorter)
Dim ec As New EntityCollection(Of ItemEntity)
DataAccessAdapter.FetchEntityCollection(ec, nothing, 200, Nothing, pp)

It gets the ItemCollection fine, but the SQL it produces does not include any "Order by ...."

Please someone release me from the agony simple_smile

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Sep-2007 10:12:41   

Dim sorter As New SortExpression() sorter.Add(New SortClause(ItemFields.Number, Nothing, SortOperator.Ascending)) pp = New PrefetchPath2(CType(EntityType.ItemEntity, Integer)) pp.Add(ItemEntity.PrefetchPathSerial, 1, Nothing, Nothing, sorter) Dim ec As New EntityCollection(Of ItemEntity) DataAccessAdapter.FetchEntityCollection(ec, nothing, 200, Nothing, pp)

The above code contain the following problems. 1- The code fetches Item entities not Serial entities, as you have described. 2- The code uses a PrefetchPath to prefetch Serial entities. 3- The code specifies a sorter to the Prefetching of the Serial. 4- The Sorter is on an ItemEntity Field, without passing a relation from Serial to Item to the PrefetchPath. 5- The code specifies 1 as the number of items to return (TOP 1).

I assume you wanted to execute the following query:

Select Serial.Field1, Serial.Field2....
From Serial
Inner Join Item ON ....
Order By Item.Number

Which can be formualted by the following: 1- Pass a Serial Collection to the FetchEntityCollection() 2- Pass a relation from SerialEntity to ItemEntity to the FetchEntityCollection() 3- Pass the SortExpression to the FetchEntityCollection()

No PrefetchPath is needed, unless you want to fetch the ItemEntity too.

ts_mjolner
User
Posts: 13
Joined: 28-Sep-2007
# Posted on: 28-Sep-2007 13:52:16   

Yes sorry the code was handwritten to state a simple example, and you are right it doesn't match up.

The suggestion you made worked fine, I had forgotten to add the relations needed.

Thx for the quick reply