This question already has an answer here:
这个问题在这里已有答案:
- LINQ: How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate] 9 answers
- LINQ:如何对集合中所有对象的属性执行.Max()并返回具有最大值的对象[重复] 9个答案
- How to use LINQ to select object with minimum or maximum property value 12 answers
- 如何使用LINQ选择具有最小或最大属性值12个答案的对象
If I have a class that looks like:
如果我有一个类似于以下的类:
public class Item
{
public int ClientID { get; set; }
public int ID { get; set; }
}
And a collection of those items...
还有这些物品的集合......
List<Item> items = getItems();
How can I use LINQ to return the single "Item" object which has the highest ID?
如何使用LINQ返回具有最高ID的单个“Item”对象?
If I do something like:
如果我这样做:
items.Select(i => i.ID).Max();
I'll only get the highest ID, when what I actually want returned is the Item object itself which has the highest ID? I want it to return a single "Item" object, not an int.
我只会得到最高的ID,当我真正想要返回的是具有最高ID的Item对象本身?我希望它返回一个“Item”对象,而不是int。
10 个解决方案
#1
114
This will loop through only once.
这将只循环一次。
Item biggest = items.Aggregate((i1,i2) => i1.ID > i2.ID ? i1 : i2);
Thanks Nick - Here's the proof
谢谢尼克 - 这是证据
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; }
}
Rearrange the list and get the same result
重新排列列表并获得相同的结果
#2
35
.OrderByDescending(i=>i.id).Take(1)
Regarding the performance concern, it is very likely that this method is theoretically slower than a linear approach. However, in reality, most of the time we are not dealing with the data set that is big enough to make any difference.
关于性能问题,这种方法很可能在理论上比线性方法慢。然而,实际上,大多数情况下,我们并没有处理足够大的数据集来做出任何改变。
If performance is a main concern, Seattle Leonard's answer should give you linear time complexity. Alternatively, you may also consider to start with a different data structure that returns the max value item at constant time.
如果表现是一个主要问题,西雅图伦纳德的答案应该给你线性时间复杂性。或者,您也可以考虑从不同的数据结构开始,该结构在固定时间返回最大值项。
#4
27
int max = items.Max(i => i.ID);
var item = items.First(x => x.ID == max);
This assumes there are elements in the items collection of course.
这假设当然物品集合中有元素。
#5
5
In case you don't want to use MoreLINQ and want to get linear time, you can also use Aggregate
:
如果您不想使用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;
This remembers the current maximal element (Item
) and the current maximal value (Item
) in an anonymous type. Then you just pick the Item
property. This is indeed a bit ugly and you could wrap it into MaxBy
extension method to get the same thing as with MoreLINQ:
这会记住匿名类型中的当前最大元素(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;
}
#6
3
Or you can write your own extension method:
或者您可以编写自己的扩展方法:
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;
}
}
#7
2
try this:
尝试这个:
var maxid = from i in items
group i by i.clientid int g
select new { id = g.Max(i=>i.ID }
#8
1
You could use a captured variable.
您可以使用捕获的变量。
Item result = items.FirstOrDefault();
items.ForEach(x =>
{
if(result.ID < x.ID)
result = x;
});
#9
1
In LINQ you can solve it the following way:
在LINQ中,您可以通过以下方式解决它:
Item itemMax = (from i in items
let maxId = items.Max(m => m.ID)
where i.ID == maxId
select i).FirstOrDefault();
#10
1
This is an extension method derived from @Seattle Leonard 's answer:
这是一种源自@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);
}
#1
114
This will loop through only once.
这将只循环一次。
Item biggest = items.Aggregate((i1,i2) => i1.ID > i2.ID ? i1 : i2);
Thanks Nick - Here's the proof
谢谢尼克 - 这是证据
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; }
}
Rearrange the list and get the same result
重新排列列表并获得相同的结果
#2
35
.OrderByDescending(i=>i.id).Take(1)
Regarding the performance concern, it is very likely that this method is theoretically slower than a linear approach. However, in reality, most of the time we are not dealing with the data set that is big enough to make any difference.
关于性能问题,这种方法很可能在理论上比线性方法慢。然而,实际上,大多数情况下,我们并没有处理足够大的数据集来做出任何改变。
If performance is a main concern, Seattle Leonard's answer should give you linear time complexity. Alternatively, you may also consider to start with a different data structure that returns the max value item at constant time.
如果表现是一个主要问题,西雅图伦纳德的答案应该给你线性时间复杂性。或者,您也可以考虑从不同的数据结构开始,该结构在固定时间返回最大值项。
#3
#4
27
int max = items.Max(i => i.ID);
var item = items.First(x => x.ID == max);
This assumes there are elements in the items collection of course.
这假设当然物品集合中有元素。
#5
5
In case you don't want to use MoreLINQ and want to get linear time, you can also use Aggregate
:
如果您不想使用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;
This remembers the current maximal element (Item
) and the current maximal value (Item
) in an anonymous type. Then you just pick the Item
property. This is indeed a bit ugly and you could wrap it into MaxBy
extension method to get the same thing as with MoreLINQ:
这会记住匿名类型中的当前最大元素(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;
}
#6
3
Or you can write your own extension method:
或者您可以编写自己的扩展方法:
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;
}
}
#7
2
try this:
尝试这个:
var maxid = from i in items
group i by i.clientid int g
select new { id = g.Max(i=>i.ID }
#8
1
You could use a captured variable.
您可以使用捕获的变量。
Item result = items.FirstOrDefault();
items.ForEach(x =>
{
if(result.ID < x.ID)
result = x;
});
#9
1
In LINQ you can solve it the following way:
在LINQ中,您可以通过以下方式解决它:
Item itemMax = (from i in items
let maxId = items.Max(m => m.ID)
where i.ID == maxId
select i).FirstOrDefault();
#10
1
This is an extension method derived from @Seattle Leonard 's answer:
这是一种源自@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);
}