简体   繁体   中英

find Max value from ObservableCollection of type KeyPair

I want to find Max Value from a ObsevableCollection Of type KeyPair... this is my KeyPair class

    public class KeyPairs : ViewModelBase
    {
    private string _key;
    public string Key
    {
        get
        {
            return _key;
        }
        set
        {
            _key = value;
            OnPropertyChanged(() => Key);
        }
    }

    private double _value;
    public double Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
            OnPropertyChanged(() => Value);
        }
    }
}

I have used

  var max = newCriteria.KeyValue.Max(values => values.Value);"

but it is not showing any result.??

so assume you have a list of KeyPair

var list = new List<KeyPair>();

// assign the data to the list
// here...

// get max value from the list
var max = list.Max(d => d.Value);

The problem is maybe in your KeyValue property. The following snippet works.

public class KeyPairs
{
    public string Key { get; set; }
    public double Value { get; set; }
}

var keyValuePairs = new List<KeyPairs>
{
    new KeyPairs {Key = "a", Value = 30},
    new KeyPairs {Key = "b", Value = 20}
};

double max = keyValuePairs.Max(pairs => pairs.Value);

Best of luck ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM