Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Linq Select Multiple properties of objects?

Tags:

c#

linq

I have a list of objects that are in the order that I want to select them, but not sure how to do it...

For example, if I have List<obj> mylist

where obj has properties:

start,
end

How do I create a list of numbers that contains

start1, end1, start2, end2, start3, end3, etc...

where start1, end1 are the int properties from the first object, start2, end2 are the number properties from the second object, and so on.. I know how to do this in a foreach loop, but not sure how to do so in Linq.

like image 962
rickster26ter1 Avatar asked Jan 18 '26 00:01

rickster26ter1


1 Answers

You can use SelectMany. Something along these lines:

var result = mylist
    .SelectMany(o => new[] { o.start, o.end })
    .ToList();

Demo @sharplab.io

like image 144
Guru Stron Avatar answered Jan 20 '26 12:01

Guru Stron



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!