IEnumerable<T>.Min(Func<T, TResult>)
首先执行转换,然后选择最小值。我可以执行任何标准的linq操作来返回原始元素吗?我得到了我可以使用Min()然后Where()来实现这一点,但是它的运行时间为O(2n)而不是最佳O(n)。说明我的意思:
var fooList = new List<Foo>
{
new Foo { Bar1 = 10, Bar2 = 0 },
new Foo { Bar1 = 8, Bar2 = 1 },
new Foo { Bar1 = 6, Bar2 = 7 }
};
var foo = fooList.Min(f => f.Bar2); // magic happens here
// foo is of type Foo with Bar1 = 10 Bar2 = 0
答案 0 :(得分:3)
没有标准运算符,但您可以使用Aggregate:
var foo = fooList.Aggregate((f1, f2)=> f1.Bar2 < f2.Bar2 ? f1 : f2);