help for converting SQL to llblgen..

Posts   
 
    
Posts: 24
Joined: 01-Dec-2006
# Posted on: 04-Dec-2006 14:27:43   

help to convert this sql to llblgen-adapter.. I want to extract one user from systemLog table where its login time is the newest and terminal name is "SqlServer".

SELECT user_id,MAX(login_date) FROM SystemLog WHERE TERMINAL = "SqlServer" GROUP BY user_id

sami
User
Posts: 93
Joined: 28-Oct-2005
# Posted on: 04-Dec-2006 15:00:47   

I think you are better off doing that with sorter and limiting the resultset, something like this:


        
        filter.Add(LogFields.UserId == someuserid);         
        filter.Add(LogFields.Terminal == 'SqlServer');  
        sorter.Add(LogFields.LoginDate | SortOperator.Descending);      
        adapter.FetchEntityCollection(collection, filterBucket, 1, sorter);


That should get the same result more efficiently.

Posts: 24
Joined: 01-Dec-2006
# Posted on: 04-Dec-2006 15:14:53   

In fact what i want to do is:

When you start the application, it should find automatically the user name looking the last loged in to the system on the same computer. So what i need is one row to extract from data base.

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 04-Dec-2006 15:16:12   

And if you want all user with only the last connection time, you can fetch an entityCollection of users and add a relation between user and systemlog. After you can apply the filter and sorter like Sami said to only have the last date for the good Terminal.

Edit: too late. The code of Sami will send you an entitycollection with only one entity so you will get your row using that code.

Posts: 24
Joined: 01-Dec-2006
# Posted on: 04-Dec-2006 15:17:53   
       ResultsetFields fields = new ResultsetFields(3);
        fields.DefineField(SistemLogFields.User_name, 0);                       
        fields.DefineField(SistemLogFields.logDate, 1);
        fields.DefineField(SistemLogFields.Terminal, 2);
        fields[1].AggregateFunctionToApply = AggregateFunction.Max;
        DataTable results = new DataTable();
        DataAccessAdapter adapter = new DataAccessAdapter();
        adapter.FetchTypedList(fields, results, null);

...

here is what i tried to do. but i wonder if there is any filter for ResultFields class. Because this will bring all the entities.

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 04-Dec-2006 15:27:50   

You can create a relationpredicatebucket(rpb). In you rpb, you add your filter : rpb.filter.Add(LogFields.Terminal == 'SqlServer')

you will use this function : FetchTypedList(IEntityFields2 fieldCollectionToFetch, DataTable dataTableToFill, IRelationPredicateBucket filterBucket, int maxNumberOfItemsToReturn, ISortExpression sortClauses, bool allowDuplicates, IGroupByCollection groupByClause)

wich will allow you to add the sort expression and the groupby clause. For the sort expression use the code that sami give you and for the groupby clause use code like this :


Dim groupByClause As IGroupByCollection = New GroupByCollection()
groupByClause.Add(fields(0))

Posts: 24
Joined: 01-Dec-2006
# Posted on: 04-Dec-2006 16:35:53   
        IPredicateExpression filter = new PredicateExpression();
        filter.Add(SistemLogFields.Terminal == sTerminalName);
        RelationPredicateBucket rpb = new RelationPredicateBucket(filter);

        DataTable results = new DataTable();            
        adapter.FetchTypedList(fields, results, rpb);

Now this is working as what i want.. thanks for help