This is a migrated thread and some comments may be shown as answers.

[Solved] Custom aggregate function retrieve column name

3 Answers 158 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jaco
Top achievements
Rank 1
Jaco asked on 08 Jul 2011, 12:04 AM
If I have a custom aggregate function like this:

public static decimal Expression<TSource>(IEnumerable<TSource> source, Func<TSource, decimal> selector)

how can I get the calling property or column name from inside the function? The source and the selector do not expose the property or column name for which this function is called. Would really appreciate if somebody can help me in anyway.

Some background on why I need this:
I use the lightweight datatable to bind to the grid. The datatable is generated based on some SQL statements. The SQL statements can differ based on some logic, meaning the datatable will look differently based on the SQL statements executed. The actual working behind the scenes are a bit more complicated, but this explanation should give the scenario. The columns retrieved also have some extra metadata. One of which is an expression like: [Sales] - [Cost] / [Cost] where the column names are in brackets.I can the have this expression on a column called [GP%]. In my aggregate function I like to get this expression metadata when the [GP%] property/column function is executed. Based on the expression I can then call some expression evaluator to calculate the formula. In the function I will just do sum(Sales), sum(Cost) and pass this to the expression evaluator to the calculation for the GP%.and return the result.

Without knowing the calling property I will not be able to get this expression metadata.

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 08 Jul 2011, 07:21 AM
Hi,

 You can use SourceField property similar to this demo

Greetings,
Vlad
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Jaco
Top achievements
Rank 1
answered on 08 Jul 2011, 02:03 PM
Hi Vlad,

If I add my function like this, specifiying the SourceField (done in autogeneratingcolumn on grid): 
ExpressionFunction expressionF = new ExpressionFunction() {SourceField= e.Column.UniqueName};
e.Column.AggregateFunctions.Add(expressionF);

How can I get the SourceField from inside the method?

public static decimal Expression<TSource>(IEnumerable source, Func<TSource, decimal> selector)
{
  //How to retrieve SourceField from source or selector??
}

Although the SourceField is specified I have no idea how to retrieve it back from inside the aggregate method. Please let me know if this is possible.

Thanks.
0
Maya
Telerik team
answered on 11 Jul 2011, 04:59 PM
Hello Jaco Jordaan,

You may try to benefit from the generic AggregateExpression. Please take a look at this forum thread and let me know whether the suggested approach may meet your requirements.
 

Best wishes,
Maya
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
GridView
Asked by
Jaco
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Jaco
Top achievements
Rank 1
Maya
Telerik team
Share this question
or