Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I Linq Select Multiple properties of objects?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

You can use SelectMany. Something along these lines:

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

Demo @sharplab.io

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading