954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

foreach iteration, get the number of iterations left?

If I have a DataRow list and use a foreach statement to create a string containing information in a list format and I want the last item in the list to be preceded by the string " and " how could I accomplish this within the foreach statement or is that not possible?

For example, if my data row contained the numbers between 1 and 10, and my foreach statement extracted the even numbers and put them in a string, I want the output of the string to be:

2, 4, 6, 8 and 10

instead of:

2, 4, 6, 8, 10

Right now I am creating a string list of locations in my locations table, here is my foreach statement as it stands now:

foreach (DataRow xLocation in locationsList.GetLocationsList)
            {
                if (L_String == string.Empty)
                {
                    L_String += xLocation["L_Name"].ToString();
                }
                else
                {
                    L_String += ", " + xLocation["L_Name"].ToString();
                }
            }


the last location should be preceded by the word "and" instead of the "," any ideas?

zachattack05
Posting Pro
516 posts since Dec 2009
Reputation Points: 61
Solved Threads: 15
 

The first thing I think to do is to just get the Length or the Count() of the list and increment your own counter and put in the "and" before the last one.

You could do it in a regular "for" loop.

I might do it something like this:

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_411840_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         List<string> lst_strSevenDwarfs = new List<string> 
            { "Doc", "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey" };

         string strSevenDwarfs = // technique 1 (keep the last comma)
            string.Join(", ", 
               lst_strSevenDwarfs.Except(
                  new List<string>{lst_strSevenDwarfs.Last()})
                  .Union(new List<string>{"and " + lst_strSevenDwarfs.Last()})
                  .ToArray());

         Console.WriteLine(strSevenDwarfs);

         strSevenDwarfs = // technique 2 (eliminate the last comma)
            string.Join(", ",
               lst_strSevenDwarfs.Except(
                  new List<string> { lst_strSevenDwarfs.Last() })
                  .ToArray()) 
                  + " and " + lst_strSevenDwarfs.Last();

         Console.WriteLine(strSevenDwarfs);
      }
   }
}
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

...or something like this:

int intLength = lst_strSevenDwarfs.Count;
         StringBuilder sb = new StringBuilder(intLength);
         
         foreach (string s in lst_strSevenDwarfs)
         {
            if ((--intLength).Equals(0))
               sb.Append("and " + s);
            else
               sb.Append(s + ", ");
         }

         Console.WriteLine(sb.ToString());
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Nice!

Thanks! Works perfectly!

zachattack05
Posting Pro
516 posts since Dec 2009
Reputation Points: 61
Solved Threads: 15
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You