GetScalar for a simple "count"....not working.

Posts   
 
    
Posts: 497
Joined: 08-Apr-2004
# Posted on: 06-Dec-2004 13:24:10   

Hi,

I have a simple bit of code, it gets the count of items in table X, where X.Group_ID = 1....


                            IPredicate countSites = PredicateFactory.CompareValue(Performance_CacheFieldIndex.Group_ID, ComparisonOperator.Equal, siteOrGroupID);
                            int sitesInGroup = (int) adaptor.GetScalar(EntityFieldFactory.Create(Performance_CacheFieldIndex.Site_ID), null, AggregateFunction.Count, countSites);

But, it returns 0. Any ideas why? I pass in "null" to GetScalar as the IExpression, because I dont think I need it, is this correct?

Ta!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 06-Dec-2004 17:03:25   

If you leave out the filter, does it work then? (i.e.: it might be the value you pass into the filter doesn't exist in the db)

Frans Bouma | Lead developer LLBLGen Pro
Posts: 497
Joined: 08-Apr-2004
# Posted on: 07-Dec-2004 12:10:26   

I realised after looking into it that the filter I was applying was too simple.

I actually have a RelationPredicateBucket that defines my relation for "sites" that belong to "site groups" - by adding the relationship to the bucket, and then specifying the filter (code below)

RelationPredicateBucket buck = new RelationPredicateBucket();
buck.Relations.Add(SiteEntity.Relations.Group_SiteEntityUsingSite_ID);
buck.PredicateExpression.Add(GeneratedGeneric.FactoryClasses.PredicateFactory.CompareValue
    (GeneratedGeneric.Group_SiteFieldIndex.AllowDeny, ComparisonOperator.Equal, "A"));
buck.PredicateExpression.Add(GeneratedGeneric.FactoryClasses.PredicateFactory.CompareValue
    (GeneratedGeneric.Group_SiteFieldIndex.Group_ID, ComparisonOperator.Equal, groupID));

What I need to do is execute my GetSclar using this RelationPredicateBucket, but as far as I can work out, I am only able to specify a predicate clause....

However.....having played around with this, I finally got the folllowing to work, is this what you would have suggested?



(using bucket defined above) 

System.Data.DataTable dt = new System.Data.DataTable();

GeneratedGeneric.HelperClasses.ResultsetFields fields = new Entropy.Envoy.GeneratedGeneric.HelperClasses.ResultsetFields(1);
fields.DefineField(SiteFieldIndex.ID, 0, "ID");
fields[0].AggregateFunctionToApply = AggregateFunction.CountRow;

adaptor.FetchTypedList(fields, dt, buck, 0, null, true, null);
return dt.Rows[0][0];

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Dec-2004 12:40:06   

There is a GetScalar (in fact, the one who does all the work, others are overloads calling this one) which accepts a relationcollection simple_smile

public virtual object GetScalar(IEntityField2 field, IExpression expressionToExecute, AggregateFunction aggregateToApply, IPredicate filter, IGroupByCollection groupByClause, IRelationCollection relations)

So pass RelationPredicateBucket.Relations as the relations parameter and RelationPredicateBucket.PredicateExpression as the filter. (but you can also create an IPredicate and pass that directly and pass a RelationCollection directly, thus not first creating a bucket)

Frans Bouma | Lead developer LLBLGen Pro
Posts: 497
Joined: 08-Apr-2004
# Posted on: 07-Dec-2004 13:22:44   

Ahhhhh! I must have just missed it, and jumped into creating a "workaround" even though I didn't have to rage

Thanks Otis simple_smile