Create an array of property values ​​from a list based on a condition

I recently used this site to get code to extract an array of property values ​​from a list of objects (I searched again and again and cannot find the original message or update help :()

This is the result:

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

The problem is that I have other properties that I output to parallel arrays to go to the data source, but would prefer to ignore any false "active" properties. Therefore, for all arrays, do something like the 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?

+3
source share
2 answers

How about this:

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

He must perform the necessary filtering.

+3
source
plan.Components.Select(c => c.qty.HasValue ? (int)c.qty.Value : 0 && (c.active == null ? false : c.active));

, , null, false

0

All Articles