aveleon
aveleon

Reputation: 167

Formatting Linq Outputs

I am trying to format the output so that that when the lines are printed it looks like this

Name          Price
LongName      Price
Name3         Price

I want it to look like a table. Here is my code

        var byValue =
            from i in invoices
            let total = (i.Quantity * i.Price)
            orderby total
            select i.PartDescription + " " + total;

        foreach (var element in byValue)
            Console.WriteLine(element);

Upvotes: 0

Views: 121

Answers (1)

Eli Arbel
Eli Arbel

Reputation: 22739

You can use String.PadRight:

select i.PartDescription.PadRight(maxLengthOfDescription) + total

If you don't know the max length, you can calculate it:

maxLengthOfDescription =
    invoices.Max(invoice => invoice.PartDescription.Length) + 1

Upvotes: 1

Related Questions