LINQ to keep adding items until a fixed length is reached?
Let's say I have a LINQ collection of
uint
some length less than 256 and I want to keep adding 0u
until it reaches length 256. If the length is already 256 or higher then this method should do nothing. Can I do that with LINQ? If so, what would be the method for that?10 Replies
What do you mean "LINQ collection"?
The length as in element count?
And I suppose you mean IEnumerable<uint>,.right?
right, element count
yeah i think
I would check the length of the list and then simply loop until its 256
Does it have to be an IEnumerable? It means that the sequence of elements is created on access - it is lazy accessed.
Behind an IEnumerable you can either have a list behind it, or a database call.
So better use a List<uint> directly
Im not sure if this is what ur looking for but this literally does that
this seems too easy of a fix so i may be
Could try something like this maybe, playing with the properties if
IEnumerable
Nah, that would enumerate the IEnumerable needlessly.
I'm gonna have to test it some
This seems to work looks like:
And no needless enumeration
Usage:
If you can work with an array/span you can do this way faster using Array.Copy
That's pretty good thanks
Agreed but this is for situations where it would be a collection coming in