Solved

Copy Member of List to Array

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

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 article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

738 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