0

i want to do the following i have variables stored in an int array called Straight i want to use Linq and get all the values when divided by 4 return 0 i tried this but it will only give me some bool variables and I'm not sure why

var a = Straight.Select(o => o % 4==0).ToArray();

any help is appreciated also i want to note that I'm still learning c# and Linq is something completely new to me

  • also i want to be able to check the length of the variable

2 Answers 2

1

The part you're looking for is Where and not Select.

var a = Straight.Where(o => (o % 4) == 0).ToArray();

Select projects your list into a new returns type which in the case of the expression (o%4) == 0 is boolean.

Where returns you the same object that fulfill the desired expression.

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

Comments

1

You need Where, not Select

var a = Straight.Where(o => o % 4 == 0).ToArray();

Select creates a projection. In your example, it turns each element of Straight into a bool.

Comments

Your Answer

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