Solved

Copy Member of List to Array

Posted on 2011-02-27
3
419 Views
Last Modified: 2013-12-17
I have some code like this.


 
struct CustomStruct
{
    int a;
    int b;
};

List<CustomStruct> myList = new List<CustomStruct>();

Open in new window


I want to copy the list of only "int a;" into a new array.

This of course will not work, but something along the lines of.

 
int[] myArray = new int[myList.Count];
myList.a.CopyToArray(myArray);

Open in new window


Thanks for you input.
Roger B.
0
Comment
Question by:_Carthmen_
3 Comments
 
LVL 10

Accepted Solution

by:
eguilherme earned 500 total points
ID: 34992219
if you are using .net 3.5 you could try:

public struct CustomStruct
{
 public   int a;
 public   int b;
};

List<CustomStruct> myList = new List<CustomStruct>();


var newArray = myList.Select(d => d.a).ToArray();
0
 

Author Closing Comment

by:_Carthmen_
ID: 34992308
Great, thanks for the help.

So fart that seems to be exactly what i needed.

I was looking at msdn for List<t> and did not see explicitly it  listed there so completely missed it.

Thanks.
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 34992357
second way to achieve this by using converter

refer to following code
struct CustomStruct
{
    public int a;
    public int b;

    
};

  public static int ReturnCustomStructA(CustomStruct customStruct)
        {
            return customStruct.a;
        }

        static void Main(string[] args)
        {
            CustomStruct customStruct1= new CustomStruct();
            customStruct1.a= 10;
            customStruct1.b=20;
            CustomStruct customStruct2= new CustomStruct();
            customStruct2.a= 11;
            customStruct2.b=20;
            List<CustomStruct> myList = new List<CustomStruct>();
            myList.Add(customStruct1);
            myList.Add(customStruct2);

            int[] values = myList.ConvertAll<int>(new Converter<CustomStruct, int>(ReturnCustomStructA)).ToArray();
}

Open in new window

0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question