Reputation:
I basically have a List that has a few columns in it. All I want to do is select whichever List item has the highest int in a column called Counted
.
List<PinUp> pinned= new List<PinUp>();
class PinUp
{
internal string pn { get; set; }
internal int pi{ get; set; }
internal int Counted { get; set; }
internal int pp{ get; set; }
}
So basically I just want pinned[whichever int has highested Count]
Hope this makes sense
The problem is i want to remove this [whichever int has highested Count] from the current list. So I have to no which int it is in the array
Upvotes: 1
Views: 2884
Reputation: 23
Try the following:
PinUp pin = pinned.OrderByDescending(x => x.Counted).First();
Upvotes: 1
Reputation: 82096
var excludingHighest = pinned.OrderByDescending(x => x.Counted)
.Skip(1);
If you need need to have a copy of the one being removed and still need to remove it you can do something like
var highestPinned = pinned.OrderByDescending(x => x.Counted).Take(1);
var excludingHighest = pinned.Except(highestPinned);
Upvotes: 2
Reputation: 460108
One way, order by it:
PinUp highest = pinned
.OrderByDescending(p => p.Counted)
.First();
This returns only one even if there are multiple with the highest Counted
. So another way is to use Enumerable.GroupBy
:
IEnumerable<PinUp> highestGroup = pinned
.GroupBy(p => p.Counted)
.OrderByDescending(g => g.Key)
.First();
If you instead just want to get the highest Counted
(i doubt that), you just have to use Enumerable.Max
:
int maxCounted = pinned.Max(p => p.Counted);
Update:
The problem is i want to remove this [whichever int has highested Count] from the current list.
Then you can use List(T).RemoveAll
:
int maxCounted = pinned.Max(p => p.Counted);
pinned.RemoveAll(p => p.Counted == maxCounted);
Upvotes: 3
Reputation:
You can order it:
var pin = pinned.OrderByDescending(p => p.Counted).FirstOrDefault();
// if pin == null then no elements found - probably empty.
If you want to remove, you don't need an index:
pinned.Remove(pin);
Upvotes: 1
Reputation: 10940
it is a sorting problem.
Sort your list by Counted
in descending order and pick the first item.
Linq has a way to do it:
var highest = pinned.OrderByDescending(p => p.Counted).FirstOrDefault();
Upvotes: 1