Print Page | Close Window

Finding a Maximum value

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3024
Printed Date: 12-Apr-2026 at 7:26am


Topic: Finding a Maximum value
Posted By: Delta_Rich
Subject: Finding a Maximum value
Date Posted: 19-Oct-2011 at 2:53pm

Once my ObservableCollection finishes loading, I wish to find out what the
Maximum value of one of the elements is.

I have used the Max() function but it does not seem to reporting the correct item

e.g. My collection is CriticalEvents and the element I am trying to get is
UpdateTracker (which is a decimal) the code thus goes like this:
LastUpdated = CriticalEvents.Max().UpdateTracker;

It does indeed return a value but it is not the maximum value of UpdateTracker
in this is collection (more like middle of the collection).

Can you please advise me of what assumption I have made that is incorrect and how to solve this issue?




Replies:
Posted By: sbelini
Date Posted: 19-Oct-2011 at 4:36pm
Hi Delta_Rich,
 
If you are simply looking for the highest UpdateTracker value, try:
 
var LastUpdated = CriticalEvents.Max(CriticalEvent => CriticalEvent.UpdateTracker);
 
Or if you want to retrieve the CriticalEvent entity with the highest UpdateTracker value, then try:
 
var LastUpdated = CriticalEvents.Where(CriticalEvent => CriticalEvent.UpdateTracker == CriticalEvents.Max(cEvent => cEvent.UpdateTracker)).First();
 
Regards,
   Silvio.


Posted By: Delta_Rich
Date Posted: 19-Oct-2011 at 8:48pm
Thanks - thus far it looks like this has solved the issue, just doing some more testing but looking good thus far (aside from the typos mixing up LastUpdated and UpdateTracker).


Posted By: sbelini
Date Posted: 20-Oct-2011 at 10:35am
I'm glad I could help and nice catch... 
I updated it for clarity.

Silvio.



Print Page | Close Window