Performing a distinct on a specific field

Posts   
 
    
gemfore
User
Posts: 35
Joined: 30-Jul-2010
# Posted on: 08-Aug-2010 15:30:05   

Sorry i'm a bit of a novice on llblgen.

Do you have any examples which include, filters , and paging via a query which will allow me to perfrom a distinct on a specific field.

Please find below my existing code...

Dim filters As New SD.LLBLGen.Pro.ORMSupportClasses.RelationPredicateBucket

        Dim productFactory As New FactoryClasses.CommerceProductEntityFactory
        Dim productEntity As New EntityClasses.CommerceProductEntity
        Dim productCollection As New HelperClasses.EntityCollection(productFactory)

        Dim sorter As New SortExpression
        sorter.Add(New SortClause(HelperClasses.CommerceProductFields.Name, Nothing, SortOperator.Descending))

        filters.PredicateExpression.Add(HelperClasses.CommerceProductFields.Active = active)
        service.FetchEntityCollection(productCollection, filters, 10, sorter, 1, 10)
        Return productCollection

This at the moment returns a collection with duplicate CategoryIds, i want to somehow show the same list, but remove such duplicates as at the moment its showing the same product 5 times as its associated to numerious categories e.g.

Select p.name, p.CompanyId, p.categoryid, p.productId from products p inner join categories c on c.id = p.categoryid

The problem I have I this will return the same product 2 or 3 times as this product is associated to many categories, but I only want the product returned once, however I need to know at least on occurance of cateoryId. So using distinct isn’t an option.

So lets say i have something like this returned e.g.

car,1,1,1 car,1,2,1 car,1,3,1 van,1,4,2 van,1,2,2

I need to just know the unique productids AND have one occurance of categoryid, any of which will do e.g.

car,1,3,1 van,1,4,2

Hope this makes sense, and any guidance would be greatly appreciated.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Aug-2010 19:21:31   

gemfore wrote:

This at the moment returns a collection with duplicate CategoryIds, i want to somehow show the same list, but remove such duplicates as at the moment its showing the same product 5 times as its associated to numerious categories e.g.

Select p.name, p.CompanyId, p.categoryid, p.productId from products p inner join categories c on c.id = p.categoryid

The problem I have I this will return the same product 2 or 3 times as this product is associated to many categories, but I only want the product returned once, however I need to know at least on occurance of cateoryId. So using distinct isn’t an option.

Please show what is the PK. It seems like it's productId, categoryId. Also show the full code snippet, including how you build the filters variable (with relations, etc).

gemfore wrote:

So lets say i have something like this returned e.g.

car,1,1,1 car,1,2,1 car,1,3,1 van,1,4,2 van,1,2,2

I need to just know the unique productids AND have one occurance of categoryid, any of which will do e.g.

car,1,3,1 van,1,4,2

The thing is: how you discriminate the correct category to return (3 and 4)? What is the SQL query that returns what you want?

David Elizondo | LLBLGen Support Team