3

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That looks like the one, i'm sure there's a few more ways but this requires hardly any refactoring. A+.
0
plan.Components.Select(c => c.qty.HasValue ? (int)c.qty.Value : 0 && (c.active == null ? false : c.active));

Take note though that it will assume that if active is null then active is false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.