1

I have a class like so:

public class MyClass
{
    public char letter { get; set; }
    public double result { get; set; }
    public bool test { get; set; }
}

I declare an array:

MyClass[] myArray = new MyClass[counter];

and fill it with some data.

I sort the array:

myArray = myArray.OrderBy(a => a.letter).ThenByDescending(a => a.result).ToArray();

Now let's say I have an int i = 100 variable.

How would I iterate through this array fields and get the index of the first element that:

  1. Has specified letter in letter field.
  2. Has test == false
  3. result < i

I'm thinking of something like this:

foreach(MyClass t in myArray.Where(a => a.letter == 'a')
{
     if(t.result < i && t.test == false) get index of that field
}

However, I'm unsure how to get the index of it. How do I do this?

2
  • 1
    First of all a char can not contain another char. ither the property is of type string or contain expression should be equals Commented Mar 4, 2015 at 13:38
  • If you need the index you should do a for instead of a foreach and do the filtering inside the for with an if. Commented Mar 4, 2015 at 13:41

4 Answers 4

4

Array.FindIndex should solve the problem for you:

int correctIndex = Array.FindIndex( myArray , item => item.letter == 'a' && item.result < i && !item.test );

The second parameter is functionally equivalent to how you would describe it in a .Where() clause.

Also, just like similar indexing functions, it returns -1 if the element isn't found.

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

Comments

2

You can do it using the overload of Select that provides an index, like this:

var res = myArray
    .Select((val, ind) => new {val, ind}))
    .Where(p => p.val.result < i && p.val.letter == 'a' && !p.val.test)
    .Select(p => p.ind);

The first Select pairs up MyClass objects, as val, with their index, as ind. Then the Where method expresses the three conditions, including the one that pairs result and ind. Finally, the last Select drops the MyClass object, because it is no longer needed.

3 Comments

From OP's question, I'm assuming that the i variable isn't an index, but just a preset filter value.
It is, if i would need to word it I'd say: find me the index of the first element in array that has letter a, test == false and result smaller than i.
@Januszoff I think you are right, this should be fixed.
1

I see the guys already did a great job answering your question with better alternatives, but just in case you still want to know how to do it with for each, here is how

int counter = 5 ; // size of your array
int i = 100 ; // the limit to filter result by
int searchResult = -1; // The index of your result [If exists]
int index = 0; // index in the array

MyClass[] myArray = new MyClass[counter]; // Define you array and fill it
myArray[0] = new MyClass {letter = 'f' ,result = 12.3 , test = false } ;
myArray[1] = new MyClass {letter = 'a' ,result = 102.3 , test = true} ;
myArray[2] = new MyClass {letter = 'a' ,result = 12.3 , test = false } ;
myArray[3] = new MyClass {letter = 'b' ,result = 88 , test = true } ;
myArray[4] = new MyClass { letter = 'q', result = 234, test = false };

myArray = myArray.OrderBy(a => a.letter).ThenByDescending(a => a.result).ToArray(); // Sort the array

foreach(MyClass t in myArray.Where(a => a.letter == 'a')) // The foreach part
{
    if (t.result < i && t.test == false)
    {
        searchResult = index;
        break;
    }
    index++;
}

// And finally write the resulting index [If the element was found]
  • Please note : Of course the resulting index will be the index in the sorted array

Comments

0

Without foreach:

var item = myArray.FirstOrDefault(e => e.letter == 'a' && e.result < i && e.test == false);
int index = Array.IndexOf(myArray, item);

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.