select ... where id=max(id) ?

Posts   
 
    
benles
User
Posts: 62
Joined: 02-May-2005
# Posted on: 17-May-2005 01:47:35   

Hi,

I'm having trouble with queries that seem simple enough to think about but hard to construct:

select * from table where id = max(id)

Is there any way to do this in the SelfService model without a GetScalar() followed by a GetMulti()?

Maybe another way to ask: How do I use an aggregate in an Expression?

I want to do something like

e = Expression(TableFieldIndex.Id, ExOp.Max);

filter.Add(PredicateFactory.CompareExpression(TableFieldIndex.Id,ComparisonOperator.Equal, e) );

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-May-2005 11:01:22   

benles wrote:

Hi,

I'm having trouble with queries that seem simple enough to think about but hard to construct:

select * from table where id = max(id)

Is there any way to do this in the SelfService model without a GetScalar() followed by a GetMulti()?

Maybe another way to ask: How do I use an aggregate in an Expression?

I want to do something like

e = Expression(TableFieldIndex.Id, ExOp.Max);

filter.Add(PredicateFactory.CompareExpression(TableFieldIndex.Id,ComparisonOperator.Equal, e) );

I think this will do the trick:


IEntityField idField = EntityFieldFactory.Create(TableFieldIndex.Id);
idField.AggregateFunctionToApply = AggregateFunction.Max;
filter.Add(PredicateFactory.CompareExpression(TableFieldIndex.Id, ComparisonOperator.Equal, new Expression(idField)) );

Frans Bouma | Lead developer LLBLGen Pro