score:5
you can use linq
's enumerable.repeat
with selectmany
:
list<int> newlist = otherlist
.selectmany(x => enumerable.repeat(x, x))
.tolist();
the selectmany
extension method takes ienumerable<t>
s selected with a given predicate and flattens them into one ienumerable<t>
. (not necessarily the same t
of the ienumerable<t>
that the method was invoked for!)
the enumerable.repeat
method, receives the amount of times to repeat a certain object
and returns an ienumerable
of that object
repeated.
score:10
linq makes this easy with selectmany
and enumerable.repeat
:
var result = input.selectmany(x => enumerable.repeat(x, x)).tolist();
the selectmany
is a flattening operation: for each input in the original sequence, generate a new sequence using the given projection... and the overall result is a sequence which contains all those "subsequences" as one flat sequence.
from there, you just need the projection - which in your case is "for an element x
, the result is x
, x
times". that's easily done with enumerable.repeat(x, x)
.
Source: stackoverflow.com
Related Query
- Creating a list with elements count dependable from the another list
- How to construct a LINQ Query to test a list against another list where elements start with the elements from the other
- Why does adding a list to another list, using add range, remove the elements from the first list?
- Get the count of distinct elements from a list of lists using LINQ
- Creating a dictionary with keys representing elements from a list
- How to iterate at elements from a sub list and then remove the sub list from the list? With great performance
- Take all the same elements from list with LINQ
- How can I update the contents of a list with data from another list?
- How to creat a new list from existing list with elements which contains the same id in Linq to object?
- How to remove multiple elements from list based on another list without creating new instance?
- Compare the item from one list object with another item from another list object
- Creating sub lists with objects of the same attribute from a list using LINQ
- Remove items of list from another lists with criteria
- How to count the number of elements that match a condition with LINQ
- Generating the Shortest Regex Dynamically from a source List of Strings
- Check if one list contains any elements from another
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- C# Sorting a List based on the sequence of values from another List (different classes)
- LINQ: Filter the list according to the math condition above elements in another list
- How do I use Linq to find the elements of a list that are not present in another list?
- Select items from a List where the children contain the items from another List using LINQ
- LINQ: delete from a list elements contained in another list
- LINQ in C#. Check if one list contains elements from another one
- C#- Select specific items from a list based on partial intersection with another list (Linq + Lambda)
- Linq to return a new object with a selection from the list
- How to Remove elements from one List based another list's element and condition?
- How can I get data from a list with a where clause to another list?
- Using LINQ to get a list of items where the item contains a part of an item from another list
- Removing certain elements from list condition based on another list
More Query from same tag
- Best way to get max value in LINQ?
- Using reflection to retrieve a value from a list
- .Net Framework 4.5.1 RemoveAt
- Linq - get max of string that ends with an integer
- See items that match in two list
- LINQ to map a datatable into a list<MyObject>
- How to process IEnumerable in batches?
- How to use Linq or Lambda with IQueryable to GroupBy and get the first/last record on the collection in C#?
- How does linq Last() determine the last item?
- LINQ - Different results with LINQ to SQL vs LINQPad
- Entity Framework - Select Single Column with Null Check
- Query on DBcontext using value object returns client evaluation explicitly in EFCore 3.1
- Linq group by one multiple properties
- Use linq to combine 2 classes into a 3rd collection
- Linq int to string
- Linq + foreach loop optimization
- XDocument.Load method does not exist in XDocument
- Linq query stored as string and/or serialized as Json
- Entity Framework: Get all rows from the table for the ids in list
- Difference between Expression<Func<>> and Func<>
- DbContext timeout while running linq query
- Combine Aggregate and Select Linq
- Adding multiple xElements at once using LINQ to XML
- LINQ get column based on another column having count >1
- Count total sum of the same product name in my ASP.NET MVC Project
- LINQ to Entities does not recognize the method
- Performance of DateTime comparison in LINQ
- Linq filtering in a property's getter
- How to dynamically add Where and Or statements to a Linq query
- Could not load file or assembly 'System.Data.Linq, Version=4.0.0.0