What is the Linq equivalent to the map! or collect! method in Ruby?
a = [ "a", "b", "c", "d" ] a.collect! {|x| x + "!" } a #=> [ "a!", "b!", "c!", "d!" ]
I could do this by iterating over the collection with a foreach, but I was wondering if there was a more elegant Linq solution.
LINQ to Lists/collection means writing the LINQ queries on list or collection. By using LINQ queries on the collection or list, we can filter or sort or remove the duplicates elements with minimal coding. Here is the syntax of writing the LINQ queries on the list or collection to get the required elements.
LINQ Where is a LINQ extension method which is used to filter the collection of elements based on the given condition? The condition can be precise as Func delegate type or in the lambda expression. This will be applicable in method syntax as well as in query syntax. In a single query, we can do multiple where extension methods.
How Where Works in LINQ? The main purpose of LINQ where is used to filter elements based on the conditions. It comes under the filtering operator category. It applies in both method and query syntax whereas method syntax requires the lambda expression and query syntax requires only the expression.
By using LINQ queries on the collection or list, we can filter or sort or remove the duplicates elements with minimal coding. Here is the syntax of writing the LINQ queries on the list or collection to get the required elements.
Map = Select
var x = new string[] { "a", "b", "c", "d"}.Select(s => s+"!");
The higher-order function map
is best represented in Enumerable.Select which is an extension method in System.Linq
.
In case you are curious the other higher-order functions break out like this:
reduce ->
Enumerable.Aggregatefilter ->
Enumerable.Where
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With