GetScalar and SELECT COUNT(*)

Posts   
 
    
Posts: 30
Joined: 17-Sep-2006
# Posted on: 01-Oct-2007 20:05:53   

Hi everyone,

I'm missing somethign blindingly obvious here. Needing to use GetScalar to fetch a count of a particular query.

if I've got

  • one field I need to do the COUNT expression on
  • a RelationPredicateBucket containing the (several) predicates and relations needed for those predicates to make sense
  • a group by expression (with a HAVING clause)

How do I use these for GetScalar?

At the moment I've got

return da.GetScalar(myField, (IExpression)null, AggregateFunction.Count, predicateBucket.PredicateExpression, groupByClause, predicateBucket.Relations);

... but this doesn't exactly match the overloads available. What am I missing?

Thanks!

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 01-Oct-2007 20:43:58   

Could you paste the complete code of your query please?

Posts: 30
Joined: 17-Sep-2006
# Posted on: 02-Oct-2007 10:36:58   
public int GetCountByTags(params string[] tags)
{


    IRelationPredicateBucket expr = new RelationPredicateBucket();
    expr.Relations.Add(BrickEntity.Relations.BrickTagMappingEntityUsingBrickId);
    expr.Relations.Add(BrickTagMappingEntity.Relations.TagEntityUsingTagId);
    expr.PredicateExpression.Add(VisibleBrickPredicate);
    expr.PredicateExpression.Add(TagFields.Name == tags);
    IGroupByCollection groupByClause = new GroupByCollection();
    groupByClause.HavingClause = new PredicateExpression(BrickTagMappingFields.BrickId.SetAggregateFunction(AggregateFunction.Count) == tags.Length);

    using (DataAccessAdapter da = new DataAccessAdapter())
    {
        return da.GetScalar(BrickFields.BrickId, null, AggregateFunction.Count, expr.PredicateExpression, groupByClause, expr.Relations);
    }       
}
Posts: 30
Joined: 17-Sep-2006
# Posted on: 02-Oct-2007 11:42:03   

Apologies - please ignore this entirely! problem solved. I was missing the blindingly obvious (GetScalar returns an object) - when I was thinking I was passing the wrong type of parameters.