Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copy Member of List to Array

Posted on 2011-02-27
3
Medium Priority
?
430 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
eguilherme earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

730 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