Typed List Group By

Posts   
 
    
kdekok
User
Posts: 26
Joined: 07-Apr-2008
# Posted on: 25-Jan-2011 15:09:29   

Maybe this is asked a lot, and i could only find an old post of 2007 where it said that this function might be added to the 2.1 version (this one: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=8952&HighLight=1) ..., but...

Why isn't there a way to add default Group By to all the non-aggegrated fields in the TypedList Editor?

Now i get the exception:


Column '...' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

And i have to add all fields except the aggregate columns to a groupbycollection everytime i use the typedlist, although the typedlist has a single use already defined in the designer.

This seems a bit odd to me.

edit:

And in addition to that. I have to derive from the typedlistclass to get access to the protected BuildResultSet() method anyway, which is needed to make a GroupByCollection. Is this by design???

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 25-Jan-2011 21:02:53   

You do need to add all of the unaggregated fields to the group by collection, I'm afraid there is no way round this. It can be done in a loop around all of the fields in the typed list, by examining if each one has an AggregateFunction and then by adding each field if it does not.

With this in mind it should be easy enough to build an extension method for TypedList which does this in a generic function.

You shouldn't need to derive from TypedListClass, you can just do

IGroupByCollection groupByClause = new GroupByCollection();

kdekok
User
Posts: 26
Joined: 07-Apr-2008
# Posted on: 26-Jan-2011 09:57:33   

Yes, that is exactly what i've done eventually:



public static class TypedListBaseExtender
{
    public static IGroupByCollection GetDefaultGroupBy<T>(this TypedListBase<T> typedlist) where T : System.Data.DataRow
    {
        GroupByCollection g = new GroupByCollection();
        foreach (IEntityField field in ((ITypedListLgp)typedlist).BuildResultset())
        {
            if (field.AggregateFunctionToApply == AggregateFunction.None) g.Add(field);
        }
        return g.Count > 0 ? g : null;
    }
}


I figured i could use the ITypedListLgp interface to get to BuildRestulSet().

I can now do this:


            DebtorGridViewTypedList debtors = new DebtorGridViewTypedList();
            debtors.Fill(0, null, false, new PredicateExpression(DebtorFields.CompanyId == mycompanyid),null, debtors.GetDefaultGroupBy());