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?

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);
      }
   }
}

...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());
commented: This is what I used, works great! +5

Nice!

Thanks! Works perfectly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.