Solved

Copy Member of List to Array

Posted on 2011-02-27
3
405 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

746 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

14 Experts available now in Live!

Get 1:1 Help Now