0

Possible Duplicate:
What is the best wayto add single element to an IEnumerable collection?

Let's say I want to concantenate Enumerable.Repeat(100, 100) and the number 3, what is the nicest way to do this?

Of course I can do Enumerable.Repeat(100, 100).Concat(Enumerable.Repeat(3,1)), but it doesn't look very expressive...

Community
  • 1
  • 1
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221

4 Answers4

3

If you really want to make it neat then the best thing to do would be to create an extension method for something like ConcatSingle and then call it.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
wizzardmr42
  • 1,634
  • 12
  • 22
2

You could use an intermediate array which contains only that element:

Enumerable.Range(100, 100).Concat(new []{ 3 });

You could also create an extension method to allow that without creating an additional array:

public static IEnumerable<T> ToEnumerable<T>(this T obj)
{
    yield return obj;
}  

Now this is possible:

Enumerable.Range(100, 100).Concat(3.ToEnumerable());
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I don't like the name of your extension method. Too close to `AsEnumerable` which does something completely different. – CodesInChaos Oct 24 '12 at 08:59
  • @CodesInChaos: Renamed to `ToEnumerable` if that's better. – Tim Schmelter Oct 24 '12 at 09:05
  • That's better, but I'm still not a fan. You don't convert the item to a sequence, you create a sequence that *contains* the item. So I still think that the name doesn't really fit its semantics. – CodesInChaos Oct 24 '12 at 09:08
  • @CodesInChaos: What do you think would be a better name? That's why i've named it `AsIEnumerable` in the first place, it doesn't really convert it to anything. It's convenient if you need to add static objects to existing sequences or queries. – Tim Schmelter Oct 24 '12 at 09:11
  • Perhaps `AsSequenceElement` or `Enumerable.FromElement` (no extension method). Personally I just go with `new[]{element}`. For the OP's use `ConcatSingle` is nicest. – CodesInChaos Oct 24 '12 at 09:16
0

You can create an extension method for converting a single value to an IEnumerable

public static class LinqEx
{
    public static IEnumerable<T> ToIEnumerable<T>(this T singleItem)
    {
        yield return singleItem;
    }
}

and then use

Enumerable.Repeat(100, 100).Concat(3.ToIEnumerable())
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
0
public static IEnumerable<T> Concat(this IEnumerable<T> source, T item)
{
    //the code here is not very expressive to you :)
}

EDIT It's suggested not to use the method name Concat, using ConcatSingle or ConcatOne instead, see the discussions in below comments.

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • The problem with this extension method is that it can break existing code. Consider the case where the item implements `IEnumerable`. – CodesInChaos Oct 24 '12 at 09:02
  • @CodesInChaos: I don't think so. You can easily tell if `T item` is a *single* object or a collection in this method. – Cheng Chen Oct 24 '12 at 09:17
  • `class X : IEnumerable{...}` => boom. While that's not the best design, introducing breaking changes with an extension method is simply unacceptable IMO. There is a reason the standard collections have both `Add` and `AddRange` instead of overloading `Add`. – CodesInChaos Oct 24 '12 at 09:20
  • @CodesInChaos: Ok..I admit what you said makes sense...because I can hardly imagine the usage scenario of `class X : IEnumerable`.. – Cheng Chen Oct 24 '12 at 09:26
  • You can use `class TreeNode:IEnumerable` to express a tree. I nowadays prefer having a `Children` property, but I think I used it in some of my earlier projects. – CodesInChaos Oct 24 '12 at 09:29
  • @CodesInChaos: Good example. I remember I used such a structure to store a tree data in the past, and like you I'd prefer a child property now. – Cheng Chen Oct 24 '12 at 09:30
  • @CodesInChaos I'm sure renaming the method to `ConcatSingle` or similar will help? – Louis Rhys Oct 24 '12 at 09:47
  • @LouisRhys Yes, naming it `ConcatSingle` is what I recommend, just like wizzardmr42's answer suggests. It's a useful method with a clear name and avoids overloading ambiguities. – CodesInChaos Oct 24 '12 at 09:57