GetScalar

Posts   
 
    
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 16-Oct-2006 19:39:23   

Hi,

I'm having alot of confusion over this function. The following will not compile...

adapter.GetScalar(LifeNstuffArticleFields.Title, LifeNstuffArticleImageFields.LifeNstuffArticleImageId == articleID, AggregateFunction.None);  

What is the difference between an IExpression, an IPredicate and an IPredicateExpression?

Why does GetScalar take an IExpression and not an IPredicateExpression and why does an expression need to be built from EntityFieldCores?

Cheers, Ian.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 16-Oct-2006 20:36:26   

What's the error you get during compilation? that would be very helpful I think wink

Frans Bouma | Lead developer LLBLGen Pro
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 17-Oct-2006 03:39:16   

The best overloaded method match for 'SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.GetScalar(SD.LLBLGen.Pro.ORMSupportClasses.IEntityField2, SD.LLBLGen.Pro.ORMSupportClasses.IExpression, SD.LLBLGen.Pro.ORMSupportClasses.AggregateFunction)' has some invalid arguments

and...

Argument '2': cannot convert from 'SD.LLBLGen.Pro.ORMSupportClasses.Predicate' to 'SD.LLBLGen.Pro.ORMSupportClasses.IExpression'

Its just not able to bind the method with the supplied arguments.

Lots of 'invalid arguments' errors!

This compiles...

adapter.GetScalar(LifeNstuffArticleFields.Title, new Expression(), AggregateFunction.None);

So I'm not just ranting. I don't see why I can't have a predicate expression here or just a predicate. And also the constructor for Expression wants entity cores which seems to be going away from the predicate system used elsewhere.

And then what is also confusing is that...

LifeNstuffArticleImageFields.LifeNstuffArticleImageId == articleID

looks just like the equivalent expressions from the docs used with this function.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 17-Oct-2006 07:25:54   

(LifeNstuffArticleImageFields.LifeNstuffArticleImageId == articleID)

is a filter not an expression. (Predicate & PredicateExpression are filters) Filters are equalities performed in the query to limit the returned result set.

On the other hand something like this: (OrderFields.ShippedDate - OrderFields.OrderDate) is an Expression. Expressions are equations that are used to manupulate the returned results.

For example (copied from the manual 'How do I...' section)

How do I get the MAX(Order.ShippingDate - Order.OrderDate) value from a SPECIFIC customer's orders ?

SELECT MAX(Order.ShippingDate - Order.OrderDate) -> That's an expression FROM Order WHERE Order.CustomerId = 10 -> That's a filter


SelfServicing

// C#
OrderCollection orders = new OrderCollection();
int maxValue = (int)orders.GetScalar(OrderFieldIndex.OrderId, (OrderFields.ShippedDate - OrderFields.OrderDate), 
    (OrderFields.CustomerId == _customerId));
' VB.NET 
Dim orders As New OrderCollection()
Dim filter As IPredicate = New FieldCompareValuePredicate(OrderFields.CustomerId, ComparisonOperator.Equal, _customerId)
Dim subExpression As New Expression(OrderFields.ShippedDate, ExOp.Sub, OrderFields.OrderDate)
Dim maxValue = CInt(orders.GetScalar(OrderFieldIndex.OrderId, subExpression, AggregateFunction.Max, filter))

' which is equal to (VB.NET 2005)
Dim orders As New OrderCollection()
Dim maxValue = CInt(orders.GetScalar(OrderFieldIndex.OrderId, (OrderFields.ShippedDate - OrderFields.OrderDate), _
    (OrderFields.CustomerId = _customerId)))

Adapter

// C#
DataAccessAdapter adapter = new DataAccessAdapter();
int maxValue = (int)adapter.GetScalar(OrderFields.OrderId, (OrderFields.ShippedDate - OrderFields.OrderDate), 
    AggregateFunction.Max, (OrderFields.CustomerId == _customerId));
' VB.NET
Dim adapter As New DataAccessAdapter()
Dim filter As IPredicate = New FieldCompareValuePredicate(OrderFields.CustomerId, Nothing, ComparisonOperator.Equal, _customerId)
Dim subExpression As New Expression(OrderFields.ShippedDate, ExOp.Sub, OrderFields.OrderDate)
Dim maxValue As Integer = CInt(adapter.GetScalar(OrderFieldIndex.OrderId, subExpression, _
    AggregateFunction.Max, filter))
    
' Which is equal to (VB.NET 2005)
Dim adapter As New DataAccessAdapter()
Dim maxValue As Integer = CInt(adapter.GetScalar(OrderFieldIndex.OrderId, (OrderFields.ShippedDate - OrderFields.OrderDate), _
    AggregateFunction.Max, (OrderFields.CustomerId == _customerId)))

Please check the reference manual for the correct GetScalar overload you want to use. And watch out for the parameters list. (The one you were trying to use had wrong parameters order)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 17-Oct-2006 10:14:34   

... and to elaborate on the difference between predicate and predicate expression: a fieldcomparevalue predicate is a predicate, a class derived from Predicate. PredicateExpression is also a class derived from predicate to which you can add predicates (i.e. other predicate expressions or predicate derived classes like Fieldcomparevalue predicate). If the method signature requires a predicateexpressin, you thus can't pass a predicate, you've to add the predicate to a predicateexpression.

Frans Bouma | Lead developer LLBLGen Pro
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 18-Oct-2006 10:20:52   

Otis wrote:

... and to elaborate on the difference between predicate and predicate expression: a fieldcomparevalue predicate is a predicate, a class derived from Predicate. PredicateExpression is also a class derived from predicate to which you can add predicates (i.e. other predicate expressions or predicate derived classes like Fieldcomparevalue predicate). If the method signature requires a predicateexpressin, you thus can't pass a predicate, you've to add the predicate to a predicateexpression.

Frans, what benefits are there for distinguishing Predicate and PredicateExpression?

Abrar
User
Posts: 26
Joined: 13-Nov-2009
# Posted on: 19-Nov-2009 14:49:22   

Hello Otis, I have a Max(Date) aggregate function. But what i read from the documentation is that it only supports numeric fields. works on numeric fields (decimal / int / float / byte / etc.) only. How to feth the most recent date or Max(Date) from the field.

Thanks, Ali

Otis wrote:

... and to elaborate on the difference between predicate and predicate expression: a fieldcomparevalue predicate is a predicate, a class derived from Predicate. PredicateExpression is also a class derived from predicate to which you can add predicates (i.e. other predicate expressions or predicate derived classes like Fieldcomparevalue predicate). If the method signature requires a predicateexpressin, you thus can't pass a predicate, you've to add the predicate to a predicateexpression.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Nov-2009 04:05:05   

Ali,

It works on Dates too. I suppose the "etc" includes that simple_smile

P.S. In the future please open a new thread so old done threads wont reopened.

David Elizondo | LLBLGen Support Team