-1

I have en enemy object:

public class Enemy { public float maximumHealth, currentHealth; }

And I have an array of this object:

public Enemy[] enemies;

And as an example the array has the following items in it:

Index maximumHealth currentHealth
0     100           77
1     100           37
2     100           59
3     100           99
4     100           11

How would I find the enemy with the highest current health (99) using Linq?

Tayab
  • 311
  • 2
  • 13
  • 1
    What's wrong with your code? – Matteo Umili Apr 11 '16 at 15:54
  • 1
    Did you at least try or are you expecting us to do it for you? The solution is easy, more a question of manners. – Toxantron Apr 11 '16 at 15:55
  • 1
    Please when upvoting question consider adding explanation why some basic search like https://www.bing.com/search?q=c%23+max+by+property+linq could not be performed by OP or maybe explain how it did not work/add good effort for OP. – Alexei Levenkov Apr 11 '16 at 15:59
  • 1
    @AlexeiLevenkov : "max by" is pretty domain specific. I'm not sure OP would necessarily have to vocabulary at hand to home in on such a search in one hit (although arguably, it would be pretty easy to get there by means of a few broader searches). – spender Apr 11 '16 at 16:04
  • @codroipo What I've done is inefficient and not worth mentioning that's why I asked help with using Linq since I don't have much knowledge about it except for that it'll provide a way to accomplish my task far more efficiently. Providing the code which I've used wouldn't have had an effect on any of the answers I've received because my question is very clear and specific so I don't see the the point. You're just a bully. – Tayab Apr 11 '16 at 16:05
  • @spender [c# array element highest value linq](https://www.bing.com/search?q=c%23%20array%20element%20highest%20property%20linq)... The only word OP may not know is "property" but I'd expect person posting on SO in C# to know class/method/property/constructor and similar names of basic constructs. – Alexei Levenkov Apr 11 '16 at 16:23
  • @Tayab you are expected to demonstrate some effort (http://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) - showing code you've already tried is generally the best way to do so, especially if code works. Note that it is *very hard* to write "element with highest property value" that is *slower* than LINQ version - so claiming that your code is "inefficient" is very hard sell in this particular case. – Alexei Levenkov Apr 11 '16 at 16:31
  • @AlexeiLevenkov Using Linq compresses the work of up to 10 lines into 1, that's the efficiency I'm talking about. I'm making a tower defense game and I'm using Linq to determine the target of various towers based of factors such as health, speed, damage, etc. – Tayab Apr 11 '16 at 16:34

2 Answers2

2

Two options. Less efficient because it requires first sorting all elements when there's only one of interest...

enemies.OrderByDescending(e => e.currentHealth).First();

or, more optimally, add a reference to MoreLinq, then:

enemies.MaxBy(e => e.currentHealth);

which is more efficient because no sort is required... MaxBy just scans the collection, replacing the best candidate with a new one if it rates better.

spender
  • 117,338
  • 33
  • 229
  • 351
2

You could do this.

Sort array in descending order and then look for First element.

enemies.OrderByDescending(s=>s.currentHealth).First()
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35