How to Implement COUNT(0) and COUNT(*) with GetScalar (Adapter)

Posts   
 
    
varalonga
User
Posts: 7
Joined: 28-Nov-2005
# Posted on: 06-Dec-2005 18:11:18   

Hi!

Sorry to bother you with what may seem a novice question (maybe it has been already answered somewhere in this forum or on the documentation, but somehow I must have missed it), but it's really bugging me. How can I implement a SELECT COUNT(0) or SELECT COUNT(*) using the GetScalar method on the Adapter scenario?

Thanks in advance.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 07-Dec-2005 02:30:07   

You can use the GetDBCount method here is a bit from the documentation for it.

// C#
OrderCollection orders = new OrderCollection();
IPredicateExpression filter = new PredicateExpression();
filter.Add(PredicateFactory.CompareValue(CustomerFieldIndex.Country, ComparisonOperator.Equal, "France"));
IRelationCollection relations = new RelationCollection();
relations.Add(OrderEntity.Relations.CustomerEntityUsingCustomerId);
int amount = orders.GetDbCount(filter, relations);
' VB.NET
Dim orders As New OrderCollection()
Dim filter As IPredicateExpression = New PredicateExpression()
filter.Add(PredicateFactory.CompareValue(CustomerFieldIndex.Country, ComparisonOperator.Equal, "France"))
Dim relations As IRelationCollection = New RelationCollection()
relations.Add(OrderEntity.Relations.CustomerEntityUsingCustomerId)
Dim amount As Integer = orders.GetDbCount(filter, relations)

The number is retrieved by using the GetScalar() method of the entity collection class, using the Count(*) aggregate function. See for more details about using Aggregate functions in your code the section Field expressions and aggregates.

swallace
User
Posts: 648
Joined: 18-Aug-2003
# Posted on: 07-Dec-2005 15:10:06   

bclubb wrote:

You can use the GetDBCount method here is a bit from the documentation for it.

// C#
OrderCollection orders = new OrderCollection();
IPredicateExpression filter = new PredicateExpression();
filter.Add(PredicateFactory.CompareValue(CustomerFieldIndex.Country, ComparisonOperator.Equal, "France"));
IRelationCollection relations = new RelationCollection();
relations.Add(OrderEntity.Relations.CustomerEntityUsingCustomerId);
int amount = orders.GetDbCount(filter, relations);
' VB.NET
Dim orders As New OrderCollection()
Dim filter As IPredicateExpression = New PredicateExpression()
filter.Add(PredicateFactory.CompareValue(CustomerFieldIndex.Country, ComparisonOperator.Equal, "France"))
Dim relations As IRelationCollection = New RelationCollection()
relations.Add(OrderEntity.Relations.CustomerEntityUsingCustomerId)
Dim amount As Integer = orders.GetDbCount(filter, relations)

The number is retrieved by using the GetScalar() method of the entity collection class, using the Count(*) aggregate function. See for more details about using Aggregate functions in your code the section Field expressions and aggregates.

I'm going to admit that this block of code has never made sense to me. I think I understand now that GetDbCount is a function that exists only in SelfServicing, not in Adapter, and since I never use SelfServicing I couldn't find GetDbCount.

Am I wrong? Is there a GetDbCount tucked away in Adapter somewhere?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 07-Dec-2005 15:24:08   

The following sample was copied from the LLBLGen Pro documentation, "Using the generated code - Field expressions and aggregates - Aggregate functions in scalar queries"

DataAccessAdapter adapter = new DataAccessAdapter();
IExpression productPriceExpression = new Expression(
    OrderDetailsFields.Quantity, ExOp.Mul, OrderDetailsFields.UnitPrice);
IPredicate filter = PredicateFactory.CompareValue(OrderDetailsFieldIndex.OrderId, ComparisonOperator.Equal, 10254);

[b]decimal orderPrice = (decimal)adapter.GetScalar(OrderDetailsFields.OrderId, 
        productPriceExpression, AggregateFunction.Sum, filter);[/b]

you can AggregateFunction.Count instead of AggregateFunction.Sum, and you can always have the Count(*) by having the Count(PK)

So mainly the following will get you going:

int nCount = (int)adapter.GetScalar(EntityNameFields.PK, AggregateFunction.Count);

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Dec-2005 15:42:29   

You also can use DataAccessAdapter.GetDbCount() (one of the 4 overloads). The GetDbCount is used for getting the count of the rowset returned by a given query. GetScalar() is a method you can also use to retrieve the db count, for example for individual fields. GetDbCount is thus another alternative simple_smile

Frans Bouma | Lead developer LLBLGen Pro