Linked Questions

14 votes
4 answers
10k views

How do I convert a single value of type T into an IEnumerable<T>? [duplicate]

There must be a good standard way of doing this, however every project I work on I have to write my own unity method, or create an inline array etc. (I hope this will quickly get closed as a ...
Ian Ringrose's user avatar
  • 51.5k
4 votes
2 answers
399 views

Is there a way to call a C# method requiring an IEnumerable<T> with a single value? ...with benchmarking [duplicate]

I'm just wondering if I'm missing out on some syntactic sugar or micro-optimization... If I'm calling a method requiring an IEnumerable of values but I only have a single value, then I put that single ...
mattica's user avatar
  • 407
69 votes
20 answers
132k views

All Possible Combinations of a list of Values

I have a list of integers List<int> in my C# program. However, I know the number of items I have in my list only at runtime. Let us say, for the sake of simplicity, my list is {1, 2, 3} Now I ...
Sach's user avatar
  • 10.2k
96 votes
12 answers
36k views

Is there a Linq method to add a single item to an IEnumerable<T>?

I am trying to do something like this: image.Layers which returns an IEnumerable<Layer> for all layers except the Parent layer, but in some cases, I just want to do: image.Layers.With(image....
Joan Venge's user avatar
  • 324k
108 votes
4 answers
96k views

Favorite way to create an new IEnumerable<T> sequence from a single value?

I usually create a sequence from a single value using array syntax, like this: IEnumerable<string> sequence = new string[] { "abc" }; Or using a new List. I'd like to hear if anyone has a more ...
Marcel Lamothe's user avatar
34 votes
5 answers
59k views

Recursive Hierarchy - Recursive Query using Linq

I am using Entity Framework (version 6) to map to a recursive hierarchy and it maps nicely. My issue is that I want to recursively get ALL child nodes of a particular node in the hierarchy. I get ...
Keith Doran's user avatar
64 votes
2 answers
8k views

Why do BCL Collections use struct enumerators, not classes?

We all know mutable structs are evil in general. I'm also pretty sure that because IEnumerable<T>.GetEnumerator() returns type IEnumerator<T>, the structs are immediately boxed into a ...
Eloff's user avatar
  • 21.3k
12 votes
8 answers
20k views

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

I'm surprised to see that there does not appear to be a method to add a single element to an IEnumerable collection. How can I add a single element to an IEnumerable collection?
Vivian River's user avatar
  • 31.8k
18 votes
4 answers
13k views

return single instance object as IEnumerable

I have in instance of class foo and i want to return it as IEnumerable. Can i do it without creating a new list etc.. Perhaps something like the following: IEnumerable<foo>.fromInstance(foo)
Adibe7's user avatar
  • 3,519
2 votes
10 answers
17k views

C# - AsEnumerable Example

What is the exact use of AsEnumerable? Will it change non-enumerable collection to enumerable collection?.Please give me a simple example.
user avatar
4 votes
6 answers
4k views

Recursion in place of multiple nested for loops?

Im having some issues with trying to update a nested for loop to use recursion instead. Is it possible to access the a,b and c variables from the earlier for loops when using recursion? Below is a ...
Hans Rudel's user avatar
  • 3,523
4 votes
4 answers
4k views

Depth-first flattened collection of an object hierarchy using LINQ

I have an object hierarchy (MasterNode -> ChildNodes) where master and child nodes are of the same type, and there are only two levels (top level and children) like this ('A' is parent of D,E and F, '...
Thanasis Ioannidis's user avatar
3 votes
4 answers
2k views

How can I enumerable one item [duplicate]

Possible Duplicates: How can I create a singleton IEnumerable? Favorite way to create an new IEnumerable<T> sequence from a single value? Say I need to return an IEnumerable which ...
user380719's user avatar
  • 9,753
3 votes
2 answers
1k views

Take single or IEnumerable parameter without an overload

Consider the following two overloaded functions. The first one just wraps the single value into a list and passes to the one that takes in multiple values. I feel like the overloaded single function ...
CuriousDeveloper's user avatar
0 votes
9 answers
3k views

Convert a string in a List<int> using LINQ (cleaner way)

I have this string: string input = "1,2,3,4,s,6"; Pay attention to the s character. I just want to convert this string in a List<int> using LINQ. I initially tried in this way: var myList = ...
Gioce90's user avatar
  • 583

15 30 50 per page