10

class String contains very useful method - String.Join(string, string[]).

It creates a string from an array, separating each element of array with a symbol given. But general - it doesn't add a separator after the last element! I uses it for ASP.NET coding for separating with "<br />" or Environment.NewLine.

So I want to add an empty row after each row in asp:Table. What method of IEnumerable<TableRow> can I use for the same functionality?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Just a new TableRow for the inserted values? – spender Jun 14 '09 at 19:40
  • How does `IEnumerable` work with the join-method that takes strings, in other words, how are you actually calling the existing method? I don't understand exactly what you are looking for here. – Lasse V. Karlsen Apr 18 '10 at 07:34
  • 1
    I don't currently call `String.Join` at all. Just looking for same functionality - inserting separator between elements of array – abatishchev Apr 18 '10 at 07:49

9 Answers9

9

The Linq equivalent of String.Join is Aggregate

For instance:

IEnumerable<string> strings;
string joinedString = strings.Aggregate((total,next) => total + ", " + next);

If given an IE of TableRows, the code will be similar.

Scott Weinstein
  • 18,890
  • 14
  • 78
  • 115
  • 4
    Warning: this might be equivalent functionally to `String.Join`, but the implementation differs. `String.Join` is smart enough to use a `StringBuilder` to avoid allocating a whole bunch of transient `String` instances, whereas the above code does not. – Kent Boogaart Mar 15 '13 at 09:29
  • @KentBoogaart That's not particularly important though, since the original question was about a generic way of replicating the behaviour, `string.Join` was just an example of a function that has that 'intersperse' behaviour. – Pharap Feb 22 '17 at 13:24
8

In .NET 3.5 you can use this extension method:

public static string Join<TItem>(this IEnumerable<TItem> enumerable, string separator)
{
   return string.Join(separator, enumerable.Select(x => x.ToString()).ToArray());
}

or in .NET 4

public static string Join<TItem>(this IEnumerable<TItem> enumerable, string separator)
{
   return string.Join(separator, enumerable);
}

BUT the question wanted a separator after each element including the last for which this (3.5 version) would work:-

public static string AddDelimiterAfter<TItem>(this IEnumerable<TItem> enumerable, string delimiter)
{
   return string.Join("", enumerable.Select(x => x.ToString() + separator).ToArray());
}

You could also use .Aggregate to do this without an extension method.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
8

I wrote an extension method:

    public static IEnumerable<T> 
        Join<T>(this IEnumerable<T> src, Func<T> separatorFactory)
    {
        var srcArr = src.ToArray();
        for (int i = 0; i < srcArr.Length; i++)
        {
            yield return srcArr[i];
            if(i<srcArr.Length-1)
            {
                yield return separatorFactory();
            }
        }
    }

You can use it as follows:

tableRowList.Join(()=>new TableRow())
spender
  • 117,338
  • 33
  • 229
  • 351
1

What you are looking for is an Intersperse function. For LINQ implementations of such a function, see this question.


Incidentally, another possible analog of String.Join is the Intercalate function, which is actually what I was looking for:

public static IEnumerable<T> Intercalate<T>(this IEnumerable<IEnumerable<T>> source,
                                            IEnumerable<T> separator) {
    if (source == null) throw new ArgumentNullException("source");
    if (separator == null) throw new ArgumentNullException("separator");
    return source.Intersperse(separator)
        .Aggregate(Enumerable.Empty<T>(), Enumerable.Concat);
}
Community
  • 1
  • 1
Michael Kropat
  • 14,557
  • 12
  • 70
  • 91
1

I find that I use this extension method a lot.

With luck, it might be incorporated into C#.

public static class Extensions
{
    public static string ToString(this IEnumerable<string> list, string separator)
    {
        string result = string.Join(separator, list);
        return result;
    }
}

Usage:

var people = new[]
{
    new { Firstname = "Bruce", Surname = "Bogtrotter" },
    new { Firstname = "Terry", Surname = "Fields" },
    new { Firstname = "Barry", Surname = "Wordsworth"}
};

var guestList = people
                    .OrderBy(p => p.Firstname)
                    .ThenBy(p => p.Surname)
                    .Select(p => $"{p.Firstname} {p.Surname}")
                    .ToString(Environment.NewLine);
Fidel
  • 7,027
  • 11
  • 57
  • 81
1

There is no built in method to do that, you should roll your own.

driis
  • 161,458
  • 45
  • 265
  • 341
  • 2
    "What method of IEnumerable ...?" This is the correct answer, albeit not that helpful. Surely not worth a downvote. – spender Jun 14 '09 at 19:39
1

If I couldn't find a method that suits my need, I would just create my own. And extension methods are very nice that way since they let you extend stuff like that. Don't know much about asp:table, but here is an extension method at least which you can tweak to whatever :p

public static class TableRowExtensions
{
    public string JoinRows(this IEnumerable<TableRow> rows, string separator)
    {
        // do what you gotta do
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Svish
  • 152,914
  • 173
  • 462
  • 620
0

If you are going to do this sort of thing frequently, then it's worth building your own extension method to do it. The implementation below allows you to do the equivalent of string.Join(", ", arrayOfStrings) where the arrayOfStrings can be an IEnumerable<T>, and separator can be any object at all. It allows you to do something like this:

var names = new [] { "Fred", "Barney", "Wilma", "Betty" };
var list = names
    .Where(n => n.Contains("e"))
    .Join(", ");

Two things I like about this are:

  1. It's very readable in a LINQ context.
  2. It's fairly efficient because it uses StringBuilder and avoids evaluating the enumeration twice.
public static string Join<TItem,TSep>(
    this IEnumerable<TItem> enuml,
    TSep                    separator)
{
    if (null == enuml) return string.Empty;

    var sb = new StringBuilder();

    using (var enumr = enuml.GetEnumerator())
    {
        if (null != enumr && enumr.MoveNext())
        {
            sb.Append(enumr.Current);
            while (enumr.MoveNext())
            {
                sb.Append(separator).Append(enumr.Current);
            }
        }
    }

    return sb.ToString();
}
Damian Powell
  • 8,655
  • 7
  • 48
  • 58
  • But it evaluates the collection twice. If that is a database query then it isn't efficient at all. Additionally, it would be more natural to lose the generic types here and make it work with strings only. – Lasse V. Karlsen Apr 18 '10 at 07:28
  • @Lasse Yes, that was a fair point. I was assuming in-memory only. Now refactored so that the enumeration is evaluated only once. – Damian Powell Apr 18 '10 at 13:03
  • Why don't you use a `foreach` statement? Right now you have a possible resouce leak. You at the very least should use a `using` statement with the `.GetEnumerator()`. – Matthew Whited Apr 18 '10 at 13:33
  • @Matthew Yes, well spotted, I missed the using statement. I can't use foreach though because that causes the enumeration to be evaluated twice (see the history of this answer for an example!) – Damian Powell Apr 18 '10 at 13:54
0

I found this post looking for the same thing, but was very unhappy with the answers (most seem to be about joining strings [?], and the accepted answer seems verbose). So I ended up just figuring out some other ways to do it.

The way I prefer is this:

public static IEnumerable<T> AfterEach <T> (this IEnumerable<T> source, T delimiter) =>
    source.SelectMany((item) => Enumerable.Empty<T>().Append(item).Append(delimiter));

Or, alternatively:

public static IEnumerable<T> AfterEach <T> (this IEnumerable<T> source, T delimiter) =>
    source.SelectMany((item) => new T[] { item, delimiter });

They're both equivalent: For each item, create a new sequence { item, delimiter }, and use SelectMany to concatenate those all together.

Another approach, which isn't a one liner but might be easier to read and is very straightforward:

public static IEnumerable<T> AfterEach <T> (this IEnumerable<T> source, T delimiter) {
    foreach (T item in source) {
        yield return item;
        yield return delimiter;
    }
}

If you don't want the extra delimiter on the end, the approach is similar, except you concatenate { delimiter, item } sequences then Skip the extra delimiter at the beginning (this is more performant than skipping one at the end).

public static IEnumerable<T> Delimit <T> (this IEnumerable<T> source, T delimiter) =>
    source.SelectMany((item) => Enumerable.Empty<T>().Append(delimiter).Append(item)).Skip(1);

public static IEnumerable<T> Delimit <T> (this IEnumerable<T> source, T delimiter) =>
    source.SelectMany((item) => new T[] { delimiter, item }).Skip(1);

The yield approach would be like this (again, two similar options, just depends on how you're feeling):

public static IEnumerable<T> Delimit <T> (this IEnumerable<T> source, T delimiter) {
    foreach (T item in source.Take(1)) // protects agains empty source
        yield return item;
    foreach (T item in source.Skip(1)) {
        yield return delimiter;
        yield return item;
    }
}

public static IEnumerable<T> Delimit <T> (this IEnumerable<T> source, T delimiter) {
    static IEnumerable<U> Helper<U> (IEnumerable<U> source, U delimiter) {
        foreach (U item in source) {
            yield return delimiter;
            yield return item;
        }
    }
    return Helper(source, delimiter).Skip(1);
}

There's some runnable examples and test code (for Delimit, not AfterEach) here. There's an implementation with Aggregate there too that I didn't think was worth writing about here. It could be adapted to AfterEach in a similar manner.

Jason C
  • 38,729
  • 14
  • 126
  • 182