Home > Software design >  ListAggregatorFactory returns system.collections.generic.list`1[system.object]
ListAggregatorFactory returns system.collections.generic.list`1[system.object]

Time:04-04

I've been using the powerful NReco library to create pivot tables. And using the very convenient HTML wrapper extension to turn those tables into HTML. When I use the '' I get the following returned into the HTML.

System.Collections.Generic.List`1[System.Object]

The error seems very straight-forward. The HTML wrapper is probably just calling ToString() on the collection object rather then joining the list. Is there a way to override the HTML wrappers functionality when it comes to formatting this list.

CodePudding user response:

I assume you mean pivot table writers (like PivotTableHtmlWriter) from NReco.PivotData.Extensions nuget package.

You're right with your suggestion that by default aggregator's value is simply converted to string with object.ToString() call. This works good for simple values, however for complex objects (including lists) there are no default representation - this motivates to define your own 'value-formatter' handler and don't rely on any default conventions:

pvtHtmlWr.FormatValue = (aggr,measureIdx) => {
  if (aggr.Count==0)
    return String.Empty;

  if (aggr.Value is IList) {
    var ls = (IList)aggr.Value;
    var sb = new StringBuilder();
    for (int i=0; i<ls.Count; i  ) {
      var entryStr = FF.Format(MeasureFormats[index],ls[i]);
      if (entryStr.Length>0) {
        if (sb.Length>0)
          sb.Append(", ");
        sb.Append(entryStr);
      }
    }
    return sb.ToString();
  }

  // apply default number format
  return String.Format("{0:0.##}", aggr.Value); 
};

More on pivot table values formatting: https://www.nrecosite.com/pivotdata/format-pivot-table.aspx

  • Related