3

I'm pretty new to LINQ and I wonder if it's possible to do the following thins:

I have an array of objects with several properties. I want to create a new array with the values of one of those properties, so if I have this class:

public class TestClass
{
    public string A {get;set;}
    public string B {get;set;}
    public string C {get;set;}
}

this is what I want to do:

public class ToDo
{
    private TestClass[] _array;

    private string[] _cProperties;

    _cProperties = queryToExtractTheValuesOfCfromTheArray_array;
}

Thanks in advance!

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • possible duplicate of [Convert a list of objects to an array of one of the object's properties](http://stackoverflow.com/questions/4765084/convert-a-list-of-objects-to-an-array-of-one-of-the-objects-properties) – Helen Jul 02 '14 at 08:15

2 Answers2

15

sure:

string[] _cProperties = _array.Select(x => x.C).ToArray();
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Ey thanks, I knwo I should had started learning LINQ way before but until now I had't the required "environment pressure" :) So thanks for your help. – Ignacio Soler Garcia Jun 09 '11 at 10:21
3
_cProperties = _array.Select(t => t.C); //.ToArray()?
spender
  • 117,338
  • 33
  • 229
  • 351