RelationPredicateBucket, filter non related items

Posts   
 
    
Jabra
User
Posts: 5
Joined: 13-May-2008
# Posted on: 13-May-2008 15:41:12   

With this code i can filter discipline items by a specific department.

How could i change the filter to list/filter only items (disciplines) which isnt related to the specified department?

Tried using:

filter.PredicateExpression.Add(DepartmentDisciplineFields.DepartmentId != departmentID);

public partial class DepartmentDisciplineManager : ManagerBase
    {
        #region "Retrieve Data"

        public static EntityCollection<DepartmentDisciplineEntity> GetDepartmentDisciplinesByDepartment(int departmentID)
        {
            //Create a new entity collection
            EntityCollection<DepartmentDisciplineEntity> departmentDisciplines = new EntityCollection<DepartmentDisciplineEntity>();


            //Add prefetch paths for necessary related data
            IPrefetchPath2 path = new PrefetchPath2(EntityType.DepartmentDisciplineEntity);
            path.Add(DepartmentDisciplineEntity.PrefetchPathDepartment);
            path.Add(DepartmentDisciplineEntity.PrefetchPathDiscipline);

            //Add a sorter
            ISortExpression sorter = new SortExpression(DepartmentDisciplineFields.DisciplineId | SortOperator.Ascending);

            //Add a filter
            IRelationPredicateBucket filter = new RelationPredicateBucket();
            if (departmentID != 0)
            {
                filter.PredicateExpression.Add(DepartmentDisciplineFields.DepartmentId == departmentID);
            }
            else
            {
                filter = null;
            }

            //Use the adapter to fetch the collection
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(departmentDisciplines, filter, 0, sorter, path);
            }

            //Return the collection
            return departmentDisciplines;
        }
        #endregion
goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 13-May-2008 22:46:27   

Try the following instead (note the "true" parameter, this is for negate the expression):

filter.PredicateExpression.Add(new FieldCompareValuePredicate(DepartmentDisciplineFields.DepartmentId, null, ComparisonOperator.Equal, departmentID,true));
Jabra
User
Posts: 5
Joined: 13-May-2008
# Posted on: 14-May-2008 11:42:29   

Thanks for the suggestion..

It does not change the return data, its still the same as before.. (Returns the existing disciplines for the selected department). Im thinking of that i might use the incorrect entity for retrieving the non-related items, or should this normally work?

I have all the existing disciplines stored in Disciplines, and the ones related to a department in DepartmentDisciplines.

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 14-May-2008 17:21:03   

Hint: Think SQL first, and when you come up with the correct query, try to implement it with LLBLGen code.

I suppose you are looking for something like:

SELECT * 
FROM Disciplines
WHERE Id NOT IN (SELECT DisciplineId FROM DepartmentDisciplines WHER DepId = xxx)

And this could be implemented using FieldCompareSetPredicate.

Jabra
User
Posts: 5
Joined: 13-May-2008
# Posted on: 20-May-2008 10:44:15   

I do get the SQL query to work, but im still unable to form the correct PredicateExpression.

Could anyone show me a simple example of a FieldCompareSetPredicate showing only entities which has not been related (yet)?

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 20-May-2008 11:33:32   

For the Adapter:

PredicateExpression filter = new PredicateExpression();
filter.Add(new FieldCompareSetPredicate(
    DisciplineFields.Id, null, DepartmentDisciplineFields.DisciplineId, null,
    SetOperator.In, (DepartmentDisciplineFields.DepartmentId == departmentID), true));

For the SelfServicing:

PredicateExpression filter = new PredicateExpression();
filter.Add(new FieldCompareSetPredicate(
    DisciplineFields.Id, DepartmentDisciplineFields.DisciplineId, 
    SetOperator.In, (DepartmentDisciplineFields.DepartmentId == departmentID), true));