Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create array of property values from list based on condition

Tags:

arrays

c#

list

linq

I've recently used this site to get the code to extract an array of property values from a list of objects (I've searched again and again and can't find the original post or help on the update :()

This is the result:

qtyArray.AddRange(plan.Components.Select(c => c.qty.HasValue ? (int)c.qty.Value : 0).ToArray());

Problem is, I have other properties i'm outputting into parallel arrays to pass to a datasource but would prefer to ignore any false 'active' properties. So for all the arrays do something like above, but only where c.active == true:

plan.Components.Select(c => c.qty.HasValue ? (int)c.qty.Value : 0 **WHERE c.active**)

Can anyone help?

like image 364
Phil Cooper Avatar asked Feb 28 '26 12:02

Phil Cooper


1 Answers

What about this:

plan.Components.Where(c => c.active).Select (c => c.qty.HasValue ? (int)c.qty.Value : 0 ) 

It should do the required filtering.

like image 156
Ole Tolshave Avatar answered Mar 02 '26 01:03

Ole Tolshave



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!