-3

I'm trying to have LINQ sort a list then remove unnecessary items on it.

class Item {
    public float time;
    public int level;

    public Item(int l, float t) {
        time = t;
        level = l;
    }
}

List<Item> items = new List<Item>();

items.Add(new Item(1, 1f));
items.Add(new Item(2, 2f));
items.Add(new Item(3, 3f));
items.Add(new Item(3, 4f));
items.Add(new Item(2, 5f));

I need to take items in this list from highest level and after time value result should be:

Item { level = 3, time = 4f }
Item { level = 2, time = 5f }

Can I do that with LINQ?

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38

1 Answers1

0

Do you need the item with the highest level value? If so:

var maxitem = items.OrderByDescending(i => i.level).FirstOrDefault();
renakre
  • 8,001
  • 5
  • 46
  • 99