Walaa wrote:
?
EntityView2 view = new EntityView2(productCollection);
ISortExpression sorter = new SortExpression(ProductVersionFields.RPPrice | SortOperator.Ascending);
sorter.Add(ProductVersionFields.MfrGrams | SortOperator.Ascending);
sorter.Add(ProductFields.Material | SortOperator.Ascending);
view.Sorter = sorter;
I don't understand whether you are trying to sort the ProductVersion entities prefetched with each Product, or you want to sort the Product Entities based on fields on the related ProductVersion entities. (which might not make sense as each Product has a collection of ProductVersions which means multiple values of Price field for instance)
The second option is exactly what I'm trying to do. The reason that it does make sense is that I want to sort the Product on the smallest price from the ProductVersion entity. I need to do this because the product could have many prices and I need to display the product with it's lowest price.
Here is some of the code I'm using now.
RelationPredicateBucket filter = new RelationPredicateBucket();
filter.Relations.Add(ProductEntity.Relations.FrameEntityUsingFrameID);
filter.Relations.Add(ProductEntity.Relations.RidingStyleEntityUsingStyleCode);
filter.Relations.Add(ProductEntity.Relations.ManufacturerEntityUsingManufacturerID);
filter.Relations.Add(ManufacturerEntity.Relations.CompanyEntityUsingManufacturerID);
filter.Relations.Add(ProductEntity.Relations.ProductVersionEntityUsingProductID);
// Lots of filtering being done here ...
ISortExpression sortVersions = new SortExpression();
sortVersions.Add(ProductVersionFields.RPPrice | SortOperator.Ascending);
sortVersions.Add(ProductVersionFields.MfrGrams | SortOperator.Ascending);
IPrefetchPathElement2 productVersionNode = prefetchPath.Add(ProductEntity.PrefetchPathProductVersion, 1, filter.PredicateExpression, filter.Relations, sortVersions);
ISortExpression sort = new SortExpression();
sort.Add(ProductVersionFields.RPPrice | SortOperator.Descending);
adapter.FetchEntityCollection(collection, filter, 0, sort, prefetchPath);
If I pass the sort on ProductVersionFields.RPPrice into the FetchEntityCollection method then the query returns a row from the Product tabel for each row of the ProductVersion table. If I don't pass the sort then the query produced by LLBLGen uses DISTINCT and a much smaller number of rows are returned. This makes the query much faster.
I was hoping that I would be able to solve this problem using the EntityView2 class when v2 was released, but now that I have it I'm still not able to solve this problem because EntityView2 will only sort on the fields from the Fields collection. I can't use EntityView2 to sort the Product entityies on the ProductVersion entity fields. I also thought that I might be able to create a property on the Product entity which returned the lowest price from the ProductVersion entity and then sort on this property, but this won't work either.
Any suggestions would be appreciated.