Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
    • ← Tools: Beyond Compare 3
    • Don’t Mix Your Serialization →

    Converting IEnumerable to a Comma-Delimited String

    02 Sep 2008 by Dean in .NET, Programming / 1 Comment

    I’m not sure whether it’s the fastest way to convert an enumerable collection of longs or ints to a comma-delimited list in C#, but it might be the shortest.

    1. IEnumerable<long> ids = new long[]{1,3,4,5};
    2. string delimitedIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());

    If you need a LINQ-free version for backward compatibility, check out Missing Functions on IEnumerable on Steve Cooper’s blog.

    Related posts

    1. 256 Character Filenames Should be Enough for Anybody
    2. Using XmlConvert for DateTime Strings
    • Tweet
    • Tags:
    • C#
    • collection
    • generic
    • LINQ

    1 Comment

    • Steve Cooper

      Hi. Thanks for the mention! I’ve found that Join is so useful on IEnumerable, I’ve written an extension function which has this signature;

      string Join(this IEnumerable sequence, string seperator);

      which means you can write;

      string delimitedids = ids.Select(x => x.ToString()).Join(“, “);

      Here’s the implementation using a stringbuilder;

      public static string Join(this IEnumerable strings, string seperator)
      {
      IEnumerator en = strings.GetEnumerator();
      StringBuilder sb = new StringBuilder();
      if (en.MoveNext())
      {
      // we have at least one item.
      sb.Append(en.Current);
      }
      while (en.MoveNext())
      {
      // second and subsequent get delimiter
      sb.Append(seperator).Append(en.Current);
      }
      return sb.ToString();
      }

      I tend to use this all the time when putting together code that generates descriptions; debug messages, ToStrings, that kind of thing.

      02 Sep 2008 09:09 pm
      Reply

      Leave a Comment

      Posting your comment...

      Subscribe to these comments via email

      • Categories

        • .NET (41)
        • AJAX (3)
        • Books (7)
        • HTML (9)
        • Infovark (8)
        • Programming (48)
        • REST (11)
        • SQL (3)
        • Testing (3)
        • Tools (13)
        • UI (3)
        • WCF (11)
        • Web Services (8)
        • WPF (4)
        • XML (4)
      • Archives

      • Get future articles


         

      • Blogroll

        • Ajaxian
        • Anne Van Kesteren
        • Brain.Save()
        • Coding Horror
        • Eric Sink
        • Joel Spolsky
        • John Resig
        • Mark Pilgrim
        • Raymond Chen
        • Scott Hansleman
        • Secret Geek
        • Steve Yegge
        • The Daily WTF
        • The Database Programmer
      • Meta

        • Log in
        • Entries RSS
        • Comments RSS
        • WordPress.org
    • Site map

      • News
      • Product
      • Download
      • Buy
      • Support
      • About
    • Recent Posts

      • Review: Brownfield Application Development in .NET
      • Using Modal Dialogs with a Splash Screen in WPF
      • Highlighting query terms in a WPF TextBlock
      • Getting XAML Hyperlink text to wrap
      • How to format the XAML Hyperlink NavigateUri
    • Twitter

      Copyright 2011 Infovark, Inc. All rights reserved.