Adapter code from sql - where in select max with group by

Posts   
 
    
hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 18-Sep-2012 17:57:15   

Hi.

I am on llblgen v3.5. I use LLBLGen Pro Runtime Framework with adapter template group.

I have this table that stores user verifications for correct access to services:

CREATE TABLE [dbo].[UserAccessLevelsVerification](
    [UserAccessLevelsVerificationId] [bigint] IDENTITY(1,1) NOT NULL,
    [UserId] [uniqueidentifier] NOT NULL,
    [ServiceId] [bigint] NOT NULL,
    [When] [datetime] NOT NULL,
 CONSTRAINT [PK_UserAccessLevelsVerification] PRIMARY KEY CLUSTERED 
(
    [UserAccessLevelsVerificationId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

I have a gridview with a llblgenprodatasource2. I'd like to add this filter to get the latest verification per service:

select * from UserAccessLevelsVerification where UserAccessLevelsVerificationId in
(select max(UserAccessLevelsVerificationId) ualvId from UserAccessLevelsVerification group by serviceid)

Below is my count/fetch code. How do I add the filter?


private RelationPredicateBucket FilterToUse { get; set; }

protected void UserAccessLevelsVerificationDsPerformGetDbCount(object sender, PerformGetDbCountEventArgs2 e) {
            using (var adapter = new DataAccessAdapter()) {
                CalculateFilterToUse();
                e.DbCount = adapter.GetDbCount(e.ContainedCollection, FilterToUse);
            }
        }

        protected void UserAccessLevelsVerificationDsPerformSelect(object sender, PerformSelectEventArgs2 e) {
            using (var adapter = new DataAccessAdapter()) {
                CalculateFilterToUse();
                var isFilterListRequest = e.PageSize == 0 && !_isExcelExport;
                var sorterToUse = isFilterListRequest ? e.Sorter : SorterToUse ?? new SortExpression(UserAccessLevelsVerificationFields.When | SortOperator.Descending);
                if (GridViewFilterFormat != CurrentFilter && e.PageNumber > 1) {
                    CurrentFilter = GridViewFilterFormat;
                    UserAccessLevelsVerificationGridView.PageIndex = 0;
                    return;
                }
                adapter.FetchEntityCollection(e.ContainedCollection, FilterToUse, e.MaxNumberOfItemsToReturn, sorterToUse, e.PrefetchPath, e.PageNumber, e.PageSize);
                _btnExcel.Enabled = e.ContainedCollection.Count > 0;
            }
        }
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 18-Sep-2012 20:15:28   

Below is my count/fetch code. How do I add the filter?

Not sure about what are you looking for. Are you looking for how to implement the IN predicate. If so, you need to look into FieldCompareSetPredicate

hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 18-Sep-2012 22:38:31   

Sure, but how do I do that with adapter?

If I try this:

FilterToUse.PredicateExpression.Add(new FieldCompareSetPredicate(UserAccessLevelsVerificationFields.UserAccessLevelsVerificationId as IEntityField, UserAccessLevelsVerificationFields.UserAccessLevelsVerificationId.SetAggregateFunction(AggregateFunction.Max) as IEntityField, SetOperator.In, null, null, string.Empty, 0, null, false, new GroupByCollection(UserAccessLevelsVerificationFields.ServiceId)));

I get this: This object was constructed using a selfservicing constructor. Can't set persistence info after that.

SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.InsertPersistenceInfoObjects(IPredicateExpression expression) +2012
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.InsertPersistenceInfoObjects(IPredicateExpression expression) +314
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PreprocessQueryElements(IRelationPredicateBucket filterBucket, IEntityFields2 fieldCollectionToFetch, ISortExpression sortClauses, IGroupByCollection groupByClause, IFieldPersistenceInfo[]& persistenceInfo, IPredicateExpression& expressionToPass) +1074
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.GetDbCount(IEntityFields2 fields, IRelationPredicateBucket filter, IGroupByCollection groupByClause, Boolean allowDuplicates) +157
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.GetDbCount(IEntityCollection2 collection, IRelationPredicateBucket filter, IGroupByCollection groupByClause) +195
   xxx.ServiceCorrectUserAccessVerificationsViewer.UserAccessLevelsVerificationDsPerformGetDbCount(Object sender, PerformGetDbCountEventArgs2 e) +183

Which FieldCompareSetPredicate ctor is used to achieve the above with adapter?

hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 18-Sep-2012 22:46:53   

Ok, this did it. Adapter does not support same FieldCompareSetPredicate ctor interface as self-servicing.

var fieldCompareSetPredicate = new FieldCompareSetPredicate(UserAccessLevelsVerificationFields.UserAccessLevelsVerificationId, null, UserAccessLevelsVerificationFields.UserAccessLevelsVerificationId.SetAggregateFunction(AggregateFunction.Max), null, SetOperator.In, null) { GroupByClause = new GroupByCollection(UserAccessLevelsVerificationFields.ServiceId) };
                FilterToUse.PredicateExpression.Add(fieldCompareSetPredicate);