1

Trying to modularise some latex table generation code, massive gold plating.

var MIDRULE = @"\\\midrule";

var a = new []{new []{"a", "b", "c"}, new []{"1", "2", "3"}, new []{"alpha", "beta", "gamma"}};

a.Dump();

a.Aggregate(
        (x,y) => x.Aggregate((i,j) => i + "&" + j) 
           + MIDRULE + Environment.NewLine
           + y.Aggregate((k,l)=>k+"&"+l)).Dump();

Expected result:

a&b&c\\\midrule
1&2&3\\\midrule
alpha&beta&gamma\\\midrule

Actual result:

Cannot implicitly convert type 'string' to 'string[]'

I'd like to do this with a one liner of aggregate commands if possible, can already do this in a nested loop or various other ways, I'm interested in getting to know aggregation better.

2 Answers 2

3

Try:

string.Concat(a.Select(inner => string.Join("&", inner)
                                + MIDRULE + Environment.NewLine))
      .Dump();

Be careful not to use Join for the outer call as you want MIDRULE + Environment.NewLine to be appended to every string including the last one.

Edit:

Since you are keen to use Aggregate here (I wouldn't recommend it as it is less readable and much more inefficient), try (untested):

a.Aggregate(string.Empty, (x, y) => x 
                                  + y.Aggregate(string.Empty,
                                                (k, l) => k + "&" + l) 
                                  + MIDRULE 
                                  + Environment.NewLine)
 .Dump();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this simple way, I would have liked to see if it was possible just using aggregate though... looks more readable to me
I added a way with Aggregate. I wouldn't go down that route if I were you.
1

Its because you are using Aggregate wrong. If you look at the definition of Aggregate you will see the definition is:

public static TSource Aggregate<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, TSource, TSource> func
)

You notice that the generic only takes one type so you are implicitly calling Aggregate which means the parameters to the function all have to be string[]. When you try to use a type string as the parameters and the expected output the compile error occurs.

Off the top of my head one way to do this would be

string.Join( MIDRULE + Environment.NewLine, a.Select(x => string.Join( "&", x))).Dump();

If you didn't want the trailing MIDRULE Newline and the already recommended

I'm not sure if this is the best way to approach this but it should work.

string.Concat(a.Select(inner => string.Join("&", inner)
                            + MIDRULE + Environment.NewLine))
  .Dump();

If you did.

1 Comment

Thanks for the extra info, i realised that I had worded the expected results wrong, you're right that I didn't want the trailing MIDRULE but i think it'd be unfair to mark this the correct answer since Ani's earlier answer outputted my (incorrect) expected results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.