List<int> newList = otherList .SelectMany(x => Enumerable.Repeat(x, x)) .ToList();
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!)
Enumerable.Repeat method, receives the amount of times to repeat a certain
object and returns an
IEnumerable of that
var result = input.SelectMany(x => Enumerable.Repeat(x, x)).ToList();
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 times". That's easily done with
- Creating a list with elements count dependable from the another list
- Creating a dictionary with keys representing elements from a list
- How to remove multiple elements from list based on another list without creating new instance?
- How to construct a LINQ Query to test a list against another list where elements start with the elements from the other
- Remove items of list from another lists with criteria
- Check if one list contains any elements from another
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- 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
- 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)
- 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?
- Removing certain elements from list condition based on another list
- creating Linq to sqlite dbml from DbLinq source code
- How to iterate at elements from a sub list and then remove the sub list from the list? With great performance
- LINQ for removing elements that are started with other element from list
- How to get Sum from List in another List as a property with linq
- Populate a large list of objects with details from another list
- Check if elements from one list elements present in another list
- Linq to get List with most elements and biggest diff inside it's elements from a List<List<int>>
- Order one list with a parameter from another list, with a joinable parameter
- Remove an object from a list based on an attribute of another list with linq
- Remove Items from sub list matching another sub list with Linq
- Update List from another List with Matching values using Linq
- Creating a List from another List, filtered by certain indexes
- How to remove elements of a list where its string contains sub strings from another list
- Take all the same elements from list with LINQ
- Get a count of elements that appear once in a list with LINQ
- Errors translating Linq expression to URI: The method 'Select' is not supported
- How to use T-SQL timestamp with Linq to Entities EF4
- Strange value in LINQ query result
- How to write a query in order to achieve the orderby descending clause for an uninitialised generic collections such as List of integers in c#?
- Finding Unique Objects between collections
- select last row from details table for each user
- How can you set an entire object using LINQ to SQL?
- How to cancel asynchronuos action in LINQ query?
- Convert SQL raw query into Linq
- How to return null if using SingleOrDefault() and searching a list of numbers for a number not in list?
- Is it possible to use OfType in a linq where?
- Can't convert string to number type in xml query
- Ordering inner keysource in simple/unnamed C# LINQ group join
- How to return specific list result from linq to entities
- Select only a single column in LINQ
- C# Linq join transforming the two entities into DTO but provide a count of one of those entities
- LINQ to Entities not supported
- Logic Evaluator in c# (Evaluate Logical (&& ,|| ) expressions)
- Upgraded to .NET Core 3.1 and receiving an error in a LINQ query related to using .FirstOrDefault()
- Group by LINQ Entity Framework