The second option is good. It first finds the max Key and then the Value that goes with it. Alternatively, you could write a MaxBy method. Or just use the one already implemented in MoreLinq: http://code.google.com/p/morelinq/
Select highest value for the key in dictionary that is highest among the keys lower than given "at" value
سؤال
Is there a better way of writing this, with LINQ instead of custom method. Sorted Dictionary is not an option. I think that Dictionary.Where(x => x.Key < at) is fine but .OrderBy(x => x.Key).Last() will be slow, as I need only one value I am wasting CPU for ordering.
Dictionary.Where(x => x.Key < at).OrderBy(x => x.Key).Last().Value;
maybe:
Dictionary[Dictionary.Where(x => x.Key < at).Max(x => x.Key)];
المحلول
نصائح أخرى
Dictionary[Dictionary.Keys.Where(key=> key < at).Max()]
لا تنتمي إلى StackOverflow