1

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

3
  • Once you have got refernce to item on list, you can delete by reference, index is not needed. Commented Aug 14, 2014 at 13:55
  • 1
    Are any of those columns unique? Commented Aug 14, 2014 at 14:00
  • Is it possible that multiple PinUps have the same Counted-value? Commented Aug 14, 2014 at 14:50

5 Answers 5

3

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);
Sign up to request clarification or add additional context in comments.

6 Comments

Tim - sort of I don't want the value of Counted, I want the item array number
@CarstenKönig I agree I could have made the question clearer but saying pinned[whichever int has highested Count] would suggest that I want the specific item from the array
@CarstenKönig no it was wrong, the OP wants to remove the item with the highest Counted value from the list. I have updated though.
@CarstenKönig it was said... "List item has the highest int in a column called "....
@CarstenKönig nope: "Returns the maximum value in a generic sequence." From: msdn.microsoft.com/en-us/library/vstudio/bb347632(v=vs.90).aspx. List doesn't have own Max, it uses LINQ extension method.
|
2
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);

4 Comments

I don't understand why he can't simply use pinned.RemoveAll(p => p.Counted==maxCounted).
@TimSchmelter well, I never said they couldn't. For me though, RemoveAll suggests that there could be more than one.
@James: yes, but if that's not the case it doesn't hurt. Otherwise it's the easiest way to remove all with the highest value. The Skip(1)/Take(1) fails if there is a second PinUp with this value, also the other approaches which try to find a single item or index.
@TimSchmelter well the question was clearly interpreted as it being a single max value (given the OPs description), as much as RemoveAll would catch matching values there is no requirement for duplicates from the OP. So either works, it's up to the OP to decide which one works for them I guess.
1

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);

1 Comment

And if multiple PinUps have this Counted?
1

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();

Comments

1

Try the following:

PinUp pin = pinned.OrderByDescending(x => x.Counted).First();

1 Comment

You would need a single selector at the end here like First/FirstOrDefault.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.