2

I have an ObservableCollection with two properties string and int. I would like to query and return the int with the Max value.

Data example:

Category, WorkOrderVersion

AAA 1

AAA 2

AAA 3

BBB 1

BBB 2

if Category == "BBB" I would want to return 2

This is kind of what I mean:

var maxWorkOrderVersion = WorkOrderDetailsObsCollection.Where(x=>x.Category == firstSelection.Category) return Max(WorkOrderVersion);`
mjordan
  • 319
  • 2
  • 22

2 Answers2

1

If you just want to get the largest WorkOrderVersion for the selected Category, then you can return it using Max:

int maxWorkOrderVersion = WorkOrderDetailsObsCollection
    .Where(x => x.Category == firstSelection.Category)
    .Max(x => x.WorkOrderVersion);

Otherwise, if you want the whole object for the specified Category that has the largest WorkOrderVersion, then you can order your list by the property (after filtering for the Category), and select the FirstOrDefault one (the default value of null would be returned in the case where no items of the specified Category exist):

var maxWorkOrderVersion = WorkOrderDetailsObsCollection
    .Where(x => x.Category == firstSelection.Category)
    .OrderByDescending(x => x.WorkOrderVersion)
    .FirstOrDefault();
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

If you want get notifications when WorkOrderDetailsObsCollection changes or Category property changes, you can use my ObservableComputations library. Using that library you can code:

var maxWorkOrderVersion = WorkOrderDetailsObsCollection
    .Filtering(x=> x.Category == firstSelection.Category)
    .Selecting(x => x.WorkOrderVersion)
    .Maximazing();

// maxWorkOrderVersion.Value is what you need

maxWorkOrderVersion is INotifyPropertyChanged. Actual value is stored in Value property. Ensure that properties mentioned in the code above notify of changes through the INotifyPropertyChanged interface.

Igor Buchelnikov
  • 505
  • 4
  • 14