C# LINQ 使用 Max() 选择单行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9114863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ Using Max() to select a single row
提问by Boggin
I'm using LINQ on an IQueryable returned from NHibernate and I need to select the row with the maximum value(s) in a couple of fields.
我在从 NHibernate 返回的 IQueryable 上使用 LINQ,我需要在几个字段中选择具有最大值的行。
I've simplified the bit that I'm sticking on. I need to select the one row from my table with the maximum value in one field.
我已经简化了我坚持的一点。我需要从我的表中选择一行,在一个字段中具有最大值。
var table = new Table { new Row(id: 1, status: 10), new Row(id: 2, status: 20) }
from u in table
group u by 1 into g
where u.Status == g.Max(u => u.Status)
select u
This is incorrect but I can't work out the right form.
这是不正确的,但我无法计算出正确的形式。
BTW, what I'm actually trying to achieve is approximately this:
顺便说一句,我实际上想要实现的大约是这样的:
var clientAddress = this.repository.GetAll()
.GroupBy(a => a)
.SelectMany(
g =>
g.Where(
a =>
a.Reference == clientReference &&
a.Status == ClientStatus.Live &&
a.AddressReference == g.Max(x => x.AddressReference) &&
a.StartDate == g.Max(x => x.StartDate)))
.SingleOrDefault();
I started with the above lambda but I've been using LINQPad to try and work out the syntax for selecting the Max().
我从上面的 lambda 开始,但我一直在使用 LINQPad 来尝试计算选择 Max() 的语法。
UPDATE
更新
Removing the GroupBy was key.
删除 GroupBy 是关键。
var all = this.repository.GetAll();
var address = all
.Where(
a =>
a.Reference == clientReference &&
a.Status == ClientStatus.Live &&
a.StartDate == all.Max(x => x.StartDate) &&
a.AddressReference == all.Max(x => x.AddressReference))
.SingleOrDefault();
采纳答案by Daniel Hilgarth
I don't see why you are grouping here.
我不明白你为什么在这里分组。
Try this:
尝试这个:
var maxValue = table.Max(x => x.Status)
var result = table.First(x => x.Status == maxValue);
An alternate approach that would iterate tableonly once would be this:
table只迭代一次的另一种方法是:
var result = table.OrderByDescending(x => x.Status).First();
This is helpful if tableis an IEnumerable<T>that is not present in memory or that is calculated on the fly.
如果table是IEnumerable<T>不存在于内存中或动态计算的,这将很有帮助。
回答by SLaks
You can group by status and select a row from the largest group:
您可以按状态分组并从最大的组中选择一行:
table.GroupBy(r => r.Status).OrderByDescending(g => g.Key).First().First();
The first First()gets the first group (the set of rows with the largest status); the second First()gets the first row in that group.
If the status is always unqiue, you can replace the second First()with Single().
第一个First()获取第一组(状态最大的行集);第二个First()获取该组中的第一行。
如果状态总是unqiue,可以更换第二First()带Single()。
回答by KAPIL SHARMA
You can also do:
你也可以这样做:
(from u in table
orderby u.Status descending
select u).Take(1);
回答by Dmitry Komin
Addressing the first question, if you need to take several rows grouped by certain criteria with the other column with max value you can do something like this:
解决第一个问题,如果您需要将按某些条件分组的多行与具有最大值的另一列一起使用,您可以执行以下操作:
var query =
from u1 in table
join u2 in (
from u in table
group u by u.GroupId into g
select new { GroupId = g.Key, MaxStatus = g.Max(x => x.Status) }
) on new { u1.GroupId, u1.Status } equals new { u2.GroupId, Status = u2.MaxStatus}
select u1;
回答by Shneor
Simplyin one line:
只需在一行中:
var result = table.First(x => x.Status == table.Max(y => y.Status));
Notice that there are two action. the inner action is for finding the max value, the outer action is for get the desired object.
请注意,有两个动作。内部动作用于寻找最大值,外部动作用于获取所需的对象。
回答by SantanaFire
More one example:
再举一个例子:
Follow:
跟随:
qryAux = (from q in qryAux where
q.OrdSeq == (from pp in Sessao.Query<NameTable>() where pp.FieldPk
== q.FieldPk select pp.OrdSeq).Max() select q);
Equals:
等于:
select t.* from nametable t where t.OrdSeq =
(select max(t2.OrdSeq) from nametable t2 where t2.FieldPk= t.FieldPk)

