Hello, I have a list that holds transactions. Currently I'm trying to write it to the console and have each transaction numbered with the transaction amount included beside it. I currently have code that produces something like
Transaction #1: $400
Transaction #1: $400
Transaction #2: $599.99
Transaction #2: $599.99
How do I stop this? Obviously my code is wrong, here is what I have that's writing to console:

foreach(decimal transaction in transactions)
{
    for(decimal i=1; i<=transactions.Count; i++)
    {
        Console.WriteLine("Transaction #{0}: ${1}", i, transaction);
    }
}

If I need to post my whole program I can, but this is where it's all happening.

okay, i'm seeing here that maybe i should not have put a decimal as my counter. that's not fixing it, but that is definitely something i should change i think.

I think I fixed it, now I just need to format it. I'll post what I've come up with.

Okay, I fixed it. Here it is.

    foreach(var transaction in transactions.Select((value, i) => new{i, value}))
    {
        Console.WriteLine("Transaction #{0}: ${1} ",transaction.i+1, transaction.value);

Edited 3 Years Ago by kiail

If anyone sees something wrong with this, please let me know. It seems to work right.

This question has already been answered. Start a new discussion instead.