Solved

Copy Member of List to Array

Posted on 2011-02-27
3
407 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now