user2706838
user2706838

Reputation: 1171

How to fill array from list of other elements in foreach?

i want to fill array of my types from list of other element. All of this I want to do in a foreach loop.

Is it possible?

How it is done now:

// Temp list
List<Parameter> inputData = new List<Parameter>();

// going through collection from which i want to copy
foreach (var parameter in parametersWindow.Parameters)
{
    inputData.Add(new Parameter() { Name = parameter.Name, Value = parameter.Value });
}

// Convertation to array.
Parameter[] parametersToInput = inputData.ToArray();

And this is how I want to do it:

Parameter[] parametersToInput = new Parameter[parametersWindow.Parameters.Count]

foreach (var param in parametersWindow.Parameters)
{
   // parametersToInput.add(new Parameter(parameter))
}

Could anyone help me?

Upvotes: 3

Views: 12055

Answers (4)

Kaf
Kaf

Reputation: 33809

If you really wan't to do a Foreach loop, you could try this:

//Your List
List<Parameter> inputData = new List<Parameter>();

//Fill Your List Here    

//Your Array
Parameter[] parametersToInput = new Parameter[inputData.Count];

//Filling Your Array from Your List
int index = 0;
inputData.ForEach(e => parametersToInput[index++] = e);

Upvotes: 1

Gusdor
Gusdor

Reputation: 14334

Am I missing something? You can do this is full LINQ with no messing around with loops.

Parameter[] parametersToInput = parametersWindow.Select(p => p.Parameters).ToArray();

On line. No drama.

If you want to perform additional processing;

Parameter[] parametersToInput = parametersWindow.Select(p => 
{
    ProcessParameters(p);
    return p.Parameters;
}).ToArray();

The Select method is ideal for 1-to-1 type transformations. With LINQ, I don't know why you would do it any other way :)

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460028

An array has no Add method since it cannot be resized. So either also use a List, use LINQ's ToArray from your list or correctly size the array and use a for-loop.

The linq approach is:

Parameter[] parametersToInput = parametersWindow.Parameters.ToArray();

The list:

List<Parameter> parametersToInput = parametersWindow.Parameters.ToList();

or

List<Parameter> parametersToInpu = new List<Parameter>(parametersWindow.Parameters);

The array-for-loop approach:

Parameter[] parametersToInput = new Parameter[parametersWindow.Parameters.Count];
for (int i = 0; i < parametersWindow.Parameters.Count; i++)
    parametersToInput[i] = parametersWindow.Parameters[i];

Update Since parametersWindow.Parameters is not a Parameter you ned to create one:

Parameter[] parametersToInput = parametersWindow.Parameters
    .Select(p => new Parameter {  Name = p.Name, Value = p.Value})
    .ToArray();

Upvotes: 5

Ahmed KRAIEM
Ahmed KRAIEM

Reputation: 10427

You can use Linq:

Parameter[] parametersToInput = parametersWindow.Parameters
                                     .Select(p => 
                                        new Parameter() { 
                                           Name = p.Name, Value = p.Value })
                                     .ToArray();

Upvotes: 0

Related Questions