Sorting and Paging

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 22-Jul-2005 01:16:29   

I'm trying to get sorting and paging working together with a typed view and the order by clause is not being created. Can anyone tell me why?

Here is my code.


public PhotoGalleryTypedView GetPhotoGallery(string manufacturer, string styleCode, int pageNumber, int pageSize)
{
    PhotoGalleryTypedView list = new PhotoGalleryTypedView();
    RelationPredicateBucket filter = new RelationPredicateBucket();
    
    if ( manufacturer != null )
        filter.PredicateExpression.Add( PredicateFactory.CompareValue(PhotoGalleryFieldIndex.Manufacturer, ComparisonOperator.Equal, manufacturer) );
    if ( styleCode != string.Empty )
        filter.PredicateExpression.Add( PredicateFactory.CompareValue(PhotoGalleryFieldIndex.StyleCode, ComparisonOperator.Equal, styleCode) );
    
    ISortExpression sort = new SortExpression();
    sort.Add( SortClauseFactory.Create( PhotoGalleryFieldIndex.Manufacturer, SortOperator.Ascending ) );
    sort.Add( SortClauseFactory.Create( PhotoGalleryFieldIndex.Model, SortOperator.Ascending ) );

    DataAccessAdapter adapter = new DataAccessAdapter();
    adapter.FetchTypedView(list.GetFieldsInfo(), list, filter, 0, sort, false, null, pageNumber, pageSize);
    return list;
}

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 22-Jul-2005 09:50:12   

You enabled tracing and hte orderby clause isn't in the query? Or the results aren't sorted as you've planned? Do you have an ORDER BY + TOP clause in your view?

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 26-Jul-2005 01:06:04   

I am looking at the query generated and it does not have the order by clause in it. Here is what is being generated by the code from my last post.


Method Enter: CreateSelectDQ (Paging)
Method Enter: CreateSelectDQ
Generated Sql query: 
    Query: SELECT DISTINCT [WS2].[dbo].[PhotoGallery].[BuildID] AS [BuildID],[WS2].[dbo].[PhotoGallery].[ProductVersionID] AS [ProductVersionID],[WS2].[dbo].[PhotoGallery].[BikeImage] AS [BikeImage],[WS2].[dbo].[PhotoGallery].[ManufacturerID] AS [CompanyID],[WS2].[dbo].[PhotoGallery].[Manufacturer] AS [Manufacturer],[WS2].[dbo].[PhotoGallery].[ManufacturerImage] AS [ManufacturerImage],[WS2].[dbo].[PhotoGallery].[ProductID] AS [ProductID],[WS2].[dbo].[PhotoGallery].[Model] AS [Model],[WS2].[dbo].[PhotoGallery].[StyleCode] AS [StyleCode] FROM [WS2].[dbo].[PhotoGallery]  WHERE ( [WS2].[dbo].[PhotoGallery].[StyleCode] = @StyleCode1) ORDER BY [WS2].[dbo].[PhotoGallery].[Manufacturer] ASC,[WS2].[dbo].[PhotoGallery].[Model] ASC
    Parameter: @StyleCode1 : AnsiStringFixedLength. Length: 2. Precision: 0. Scale: 0. Direction: Input. Value: R.
Method Exit: CreateSelectDQ
Generated Sql query: 
    Query: CREATE TABLE #TempTable ([__rowcnt][int] IDENTITY (1,1) NOT NULL,[BuildID][Int] NULL,[ProductVersionID][Int] NULL,[BikeImage][VarChar](50) NULL,[CompanyID][Int] NULL,[Manufacturer][VarChar](50) NULL,[ManufacturerImage][VarChar](50) NULL,[ProductID][Int] NULL,[Model][VarChar](50) NULL,[StyleCode][Char](2) NULL);INSERT INTO #TempTable ([BuildID],[ProductVersionID],[BikeImage],[CompanyID],[Manufacturer],[ManufacturerImage],[ProductID],[Model],[StyleCode]) SELECT DISTINCT TOP 5 [WS2].[dbo].[PhotoGallery].[BuildID] AS [BuildID],[WS2].[dbo].[PhotoGallery].[ProductVersionID] AS [ProductVersionID],[WS2].[dbo].[PhotoGallery].[BikeImage] AS [BikeImage],[WS2].[dbo].[PhotoGallery].[ManufacturerID] AS [CompanyID],[WS2].[dbo].[PhotoGallery].[Manufacturer] AS [Manufacturer],[WS2].[dbo].[PhotoGallery].[ManufacturerImage] AS [ManufacturerImage],[WS2].[dbo].[PhotoGallery].[ProductID] AS [ProductID],[WS2].[dbo].[PhotoGallery].[Model] AS [Model],[WS2].[dbo].[PhotoGallery].[StyleCode] AS [StyleCode] FROM [WS2].[dbo].[PhotoGallery]  WHERE ( [WS2].[dbo].[PhotoGallery].[StyleCode] = @StyleCode1) ORDER BY [WS2].[dbo].[PhotoGallery].[Manufacturer] ASC,[WS2].[dbo].[PhotoGallery].[Model] ASC;SELECT [BuildID],[ProductVersionID],[BikeImage],[CompanyID],[Manufacturer],[ManufacturerImage],[ProductID],[Model],[StyleCode] FROM #TempTable WHERE [__rowcnt] > @__rownoStart AND [__rowcnt] <= @__rownoEnd;DROP TABLE #TempTable
    Parameter: @StyleCode1 : AnsiStringFixedLength. Length: 2. Precision: 0. Scale: 0. Direction: Input. Value: R.
    Parameter: @__rownoStart : Int32. Length: 4. Precision: 10. Scale: 0. Direction: Input. Value: 0.
    Parameter: @__rownoEnd : Int32. Length: 4. Precision: 10. Scale: 0. Direction: Input. Value: 4.
Method Exit: CreateSelectDQ (Paging)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 26-Jul-2005 09:42:18   

Yes it does: "ORDER BY [WS2].[dbo].[PhotoGallery].[Manufacturer]" The query for a paging action is a 3-step process. 1) create the temptable 2) do an insert into temptable... select .. from ... In this query the order by is there, I copied the string above from that query 3) then do a straight select * from temptable for the page data. This query doesn't have the orderby, as it already is in the right order, inserted in step 2).

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 26-Jul-2005 18:53:54   

Otis,

Your right it is there now. I am a bit confused as I have been trying to get the results of this query to sort correctly for a while and as soon as I post the question it works. Of course this isn't the first time I have had something not work, posted a question and then found that it works and I haven't changed anything.

Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 26-Jul-2005 19:20:42   

Perhaps you commented out a line or so which caused it to not being included... The query generator is to be deterministic, so when you put in the same values again, it should generate the same query, if not it's a bug.

Please inform me a.s.a.p. if you run into a routine which shows undeterministic behavior with the query generator.

Frans Bouma | Lead developer LLBLGen Pro