public static StringTable ToTable <T>(this IEnumerable <T> enumerable, TableFormat format, bool derived = true) { var properties = enumerable.Select(item => typeof(T) .GetPropertiesForTable() .Concat(derived ? item.GetType().GetPropertiesForTable() : Enumerable.Empty <PropertyInfo>() ) .GroupBy(prop => prop.Name) .Select(props => props.First()) ); return(new StringTable( properties .Transpose() .Select(props => string.Join("/", props .Where(col => col != null) .Select(prop => prop.Name.SplitCamelCase()) .Distinct() ) ), properties .Zip(enumerable, (props, obj) => new { Properties = props, Object = obj } ) .Select(item => item.Properties .Select(prop => prop?.GetValue(item.Object)) ), format )); }
public StringTable(IEnumerable <string> columns, IEnumerable <IEnumerable <object> > rows, TableFormat format) { Columns = columns; Rows = rows.Select(row => row .Concat(Enumerable.Repeat <object>( null, Math.Max(Columns.Count() - row.Count(), 0)) ) .Select(cell => cell?.ToString() ?? "") ); Format = format; }