newbieguy
newbieguy

Reputation: 697

Find all elements that are lesser than or equal to the value

I'm new in c# and I have an array like this:

int[] cost = new int[] {10, 15, 20, 30, 50};

What is the simplest code to find/get all values that are lesser than or equal to the value?

The given value is 29, would return 20, 15, and 10.

Upvotes: 0

Views: 9795

Answers (1)

DWright
DWright

Reputation: 9500

Here's one of the simplest things you can do for this:

var resultArray = cost.Where(item => item <= 29).ToArray();

Here's another:

var resultArray = Array.FindAll(cost, item => item <= 29);
  1. The first option uses an extension methods named Where that operates on IEnumerable, which arrays implement. IEnumerable is an interface for a sequence of elements that can be stepped through one after the other.

  2. The second option uses FindAll, which is actually built in to the Array class.

The item => item <= 29 stuff is lambda notation. It's a highly concise way of defining a function on the spot.

This way of writing an anonymous function on the spot is saying

  • Where or FindAll each receive an argument that is an anonymous function, and we will define that argument that is an anonymous function right here in the actual call to Where() or FindAll().
  • The anonymous function itself receives as its own one argument an element, which we'll call item (the name could be anything, it could also be x). This item is an individual element of the Array (or IEnumerable)--elements being ints in this case--which the compiler infers. The name of the function argument is left of the =>
  • the function body is right of the =>. (Read => as "this is a function where the argument item goes to the body right of the => ") The body is an expression that evaulates to a Boolean true or a false, so the return value of the function is a Boolean.

So, essentially item => item <= 29 is the same as declaring a function like the below, but it's much more concise and can be easily used as an argument to another function. Here's the long way of declaring such a function, so you can see that the lambda way of writing it is much more concise:

Boolean int func(int x)
{
    if (x<=29) {
        return true;
    } else {
      return false;
    }
}
  • the Where or Array.FindAll functions call the lambda function on each element in the Array or IEnumerable and return (yield) back only those items where the lambda function returns true.

Update

Looks like you edited the original question to remove a reference to finding indexes, so the below is no longer relevant (the above stuff finds values of elements, not indexes of elements). But the below shows how to find both values and indexes of the array elements that satisfy the condition:

You mentioned indexes. This version also finds the indexes of the original array (not just the elements) that satisfy the criterion:

List<int> indexesInvolved = new List<int>();
var resultArray = cost.Where((item, index) =>
    { 
        if (item <= 29) {
            indexesInvolved.Add(index);
            return true;
        } 
        else {
            return false;
        } 
    }
 ).ToArray();
var foundIndexArray = indexesInvolved.ToArray();

Upvotes: 9

Related Questions