score:7
score:0
There are two different approaches - you can use Select extension method or you can use select clause:
var numbers = new List<int>();
var strings1 = from num in numbers select ToWords(num);
var strings2 = numbers.Select(ToWords);
both of them will return IEnumerable<>
, which you can cast as you need (for example, with .ToArray()
or .ToList()
).
score:0
You could do something like this :
public static string[] ProcessStrings(int[] intList)
{
return Array.ConvertAll<int, string>(intList, new Converter<int, string>(
delegate(int number)
{
return ToWords();
}));
}
If it is a list then :
public static List<string> ProcessStrings(List<int> intList)
{
return intList.ConvertAll<string>(new Converter<int, string>(
delegate(int number)
{
return ToWords();
}));
}
score:0
Straight simple:
string[] wordsArray = array.ToList().ConvertAll(n => ToWords(n)).ToArray();
If you are OK with Lists, rather than arrays, you can skip ToList()
and ToArray()
.
Lists are much more flexible than arrays, I see no reason on earth not to use them, except for specific cases.
score:7
It's pretty straight forward. Just use the Select
method:
var results = array.Select(ToWords).ToArray();
Note that unless you need an array you don't have to call ToArray
. Most of the time you can use lazy evaluation on an IEnumerable<string>
just as easily.
Source: stackoverflow.com
Related Query
- Performing function on each array element, returning results to new array
- C#/Linq: Apply a mapping function to each element in an IEnumerable?
- How to .ToUpper() each element of an array using LINQ?
- How do I get first element of each array via Linq C#
- Finding an element with XDocument returning zero results
- Using IEnumerable methods, perform operation on first and second element of list, then second and third, etc, returning result as new IEnumerable
- how to replace each element (of struct type) with another element in an array using Linq?
- In C# Groupby Select using a ForEach or other function to aggregate one column into a array of results
- Trouble with LINQ Returning Results Set from Object[] Array Within Object
- List or Array of String Contain specific word in Html Source Code
- Returning LINQ row results to Array or List
- Fetch element with common id among two array but id must be unique for each sets of arrays in C#
- Trouble Returning XML to 2D Array not Looping to next Element
- call a function for each value in a generic c# collection
- LinqPad Not Returning Results With C# Statements
- Initializing a C# array with multiple copies of the same element
- Can I rely on the LINQ ToArray() always returning a new instance?
- Flatten List<string[]> into single string with one line for each element
- LINQ - array property contains element from another array
- Rules of thumb for when to call ToList when returning LINQ results
- Comparing each element with each other element in a list
- Accessing Results View from variable in code
- Select every second element from array using lambda
- LINQ select to new object, setting values of object in function
- Add quotes at the beginning and end of each element in list using LINQ
- Combine the results of two columns in a select into one array with LINQ?
- C# MongoDB driver only returning 100 results
- linq query for varchar field not returning any results
- Using LINQ to delete an element from a ObservableCollection Source
- LINQ Source Code Available
More Query from same tag
- Optimize Entity framework Query, avoid lazy-loading
- Multiple select after grouping in Linq Query
- Custom OR function in linq
- C# Determing whether any element in a string array contains a given string anywhere
- How to group a Dictionary by DateTime and average results using Linq
- How to use my view model class and passing data from view model to View on the page?
- C# linq using null or empty strings in a where statment
- Difference between System.Linq.Dynamic and System.Linq?
- Accessing attributes on fields using extension method
- Comparison between select then filtering and select distinct in linq, c#
- linq-sql join two tables and select columns
- How can i enable Transaction my codes with linqto SQL?
- LinqToSql with dates problem
- Is this a FormView bug?
- Empty response when using DbGeography distance and Entity Framework
- How to use selectmany with condition in c#
- LINQ to XML At least one object must implement IComparable
- VB.NET LINQ Query: Getting The Sum of All Values For A Specific Structure Member
- C# Linq select and cast without error
- C# Parsing simple XML file with LINQ
- entity framework / linq query construction
- group a list and only get distinct values when input is a list
- nhibernate query with child entities and lazy="false"
- Why implement IEqualityComparer<T> in a separate class
- How can I iterate over a collection and change values with LINQ extension methods?
- Getting distinct not null dates from datetime collection
- EF Core - How to group by a nullable column through multiple joins
- Find count of an list2.item in list1
- Using value from related table in LINQ where clause
- What is the best practice for querying related tables using LINQ given the following example?