如何让 LINQ 返回给定属性的最大值的对象?

如果我有一个类是这样的:

public class Item
{
public int ClientID { get; set; }
public int ID { get; set; }
}

这些东西的集合。

List<Item> items = getItems();

如何使用 LINQ 返回 ID 最高的单个“ Item”对象?

如果我这样做:

items.Select(i => i.ID).Max();

我只能得到最高的 ID,而我实际上想要返回的是具有最高 ID 的 Item 对象本身?我希望它返回一个“ Item”对象,而不是一个 int。

164285 次浏览
.OrderByDescending(i=>i.id).First()

关于性能问题,这种方法在理论上很可能比线性方法慢。然而,在现实中,大多数时候我们并没有处理足够大的数据集来产生任何不同。

如果性能是一个主要问题,西雅图莱纳德的答案应该给你线性时间复杂性。或者,您也可以考虑从一个不同的数据结构开始,该结构在常数时间返回 max 值项。

First()将执行与 Take(1)相同的操作,但直接返回该项,而不是返回包含该项的枚举。

使用 Morelinq项目中的 MaxBy:

items.MaxBy(i => i.ID);

试试这个:

var maxid = from i in items
group i by i.clientid int g
select new { id = g.Max(i=>i.ID }
int max = items.Max(i => i.ID);
var item = items.First(x => x.ID == max);

当然,这里假设项集合中有元素。

如果你不想使用 MoreLINQ 并且想得到线性时间,你也可以使用 Aggregate:

var maxItem =
items.Aggregate(
new { Max = Int32.MinValue, Item = (Item)null },
(state, el) => (el.ID > state.Max)
? new { Max = el.ID, Item = el } : state).Item;

这会记住匿名类型中的当前最大元素(Item)和当前最大值(Item)。然后选择 Item属性。这确实有点难看,你可以把它包装到 MaxBy扩展方法中,得到与 MoreLINQ 相同的东西:

public static T MaxBy(this IEnumerable<T> items, Func<T, int> f) {
return items.Aggregate(
new { Max = Int32.MinValue, Item = default(T) },
(state, el) => {
var current = f(el.ID);
if (current > state.Max)
return new { Max = current, Item = el };
else
return state;
}).Item;
}

这只会循环一次。

Item biggest = items.Aggregate((i1,i2) => i1.ID > i2.ID ? i1 : i2);

谢谢,尼克,这就是证据

class Program
{
static void Main(string[] args)
{
IEnumerable<Item> items1 = new List<Item>()
{
new Item(){ ClientID = 1, ID = 1},
new Item(){ ClientID = 2, ID = 2},
new Item(){ ClientID = 3, ID = 3},
new Item(){ ClientID = 4, ID = 4},
};
Item biggest1 = items1.Aggregate((i1, i2) => i1.ID > i2.ID ? i1 : i2);


Console.WriteLine(biggest1.ID);
Console.ReadKey();
}




}


public class Item
{
public int ClientID { get; set; }
public int ID { get; set; }
}

重新排列列表,得到相同的结果

或者你可以编写自己的扩展方法:

static partial class Extensions
{
public static T WhereMax<T, U>(this IEnumerable<T> items, Func<T, U> selector)
{
if (!items.Any())
{
throw new InvalidOperationException("Empty input sequence");
}


var comparer = Comparer<U>.Default;
T   maxItem  = items.First();
U   maxValue = selector(maxItem);


foreach (T item in items.Skip(1))
{
// Get the value of the item and compare it to the current max.
U value = selector(item);
if (comparer.Compare(value, maxValue) > 0)
{
maxValue = value;
maxItem  = item;
}
}


return maxItem;
}
}

您可以使用捕获的变量。

Item result = items.FirstOrDefault();
items.ForEach(x =>
{
if(result.ID < x.ID)
result = x;
});

在 LINQ 中,你可以用下面的方法来解决这个问题:

Item itemMax = (from i in items
let maxId = items.Max(m => m.ID)
where i.ID == maxId
select i).FirstOrDefault();

这是一种扩展方法,源自@Seattle Leonard 的回答:

 public static T GetMax<T,U>(this IEnumerable<T> data, Func<T,U> f) where U:IComparable
{
return data.Aggregate((i1, i2) => f(i1).CompareTo(f(i2))>0 ? i1 : i2);
}