0

I have this array :

 Point[] arr = samples.pointsArray; 

I using this row to retrieve all elements that satisfy condition:

var maxXCol = arr.Where( p => maxX.X == p.X ); 

Any idea how to modify row above, to get only the indexes of these elements?

Thank you in advance!

6 Answers 6

2

EDIT

Use the version of Select that takes both the index and the object and create an anonymous object with the object and index inside it. It would look like this:

someEnumerable.Select((obj, idx) => new {Item = obj, Index = idx}) 

You'll need to do this before you use Where so that the original index remains intact after the filter operation.

In the following operations you can use the item like so:

x => x.Item 

and the index like so:

x => x.Index 
Sign up to request clarification or add additional context in comments.

1 Comment

Note to downvoters: both Honza's and my answer were enough information to correctly solve the problem, but contained minor (easy to fix on your own) errors. Next time use a comment to indicate this!
1
var maxXCol = arr.Select((p, inx) => new { p,inx}) .Where(y => maxX.X == y.p.X) .Select(z => z.inx); 

Comments

1

You may select the value first with the index in anonymous type, later you filter it with your condition and then select the index.

var result = arr.Select((g, index) => new { g, index }) .Where(r => maxX.X == r.X) .Select(t => t.index); 

Comments

1

You can use Select overload which takes an index, and project that index together with the original row. Then take only the index for the result collection.

var maxXCol = arr .Select((p, index) => new { Item = p, Index = index }) .Where(p => maxX.X == p.Item.X) .Select(x => x.Index); 

2 Comments

Well, at least I can fix your unfortunate loss of points...GL next time...That'll teach us to post a useful answer with minor mistakes! ;)
Thanks, did the same for you. Yup, we should proofread better next time before posting :)
1

Try this:

arr.Select((e,i)=>new{index=i, value=e}).Where(ei=>ei.value.X==maxX.X).Select(ei=>ei.index); 

Comments

1
var maxXCol = arr .Select((a, b) => new { b, a }) .Where(p => maxX.X == p.a.X) .Select(i=>i.b); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.