Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible with LINQ to extract from an array of objects the value of one property and create a new array?

Tags:

c#

.net

linq

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!

like image 596
Ignacio Soler Garcia Avatar asked Dec 02 '25 10:12

Ignacio Soler Garcia


1 Answers

sure:

string[] _cProperties = _array.Select(x => x.C).ToArray();
like image 73
Daniel Hilgarth Avatar answered Dec 05 '25 01:12

Daniel Hilgarth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!