LINQ使用Max()选择单行

时间:2012-02-02 15:22:04

标签: c# .net linq

我在从NHibernate返回的IQueryable上使用LINQ,我需要在几个字段中选择具有最大值的行。

我已经简化了我坚持的那一点。我需要从表中选择一行,并在一个字段中显示最大值。

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

这是不正确的,但我无法找到正确的表格。

顺便说一句,我实际上要实现的目标大致是这样的:

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();

我从上面的lambda开始,但我一直在使用LINQPad来尝试找出选择Max()的语法。

更新

删除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();

6 个答案:

答案 0 :(得分:205)

我不知道你为什么要在这里分组。

试试这个:

var maxValue = table.Max(x => x.Status)
var result = table.First(x => x.Status == maxValue);

只会迭代table一次的替代方法是:

var result = table.OrderByDescending(x => x.Status).First();

如果tableIEnumerable<T>在内存中不存在或即时计算,则此功能非常有用。

答案 1 :(得分:14)

你也可以这样做:

(from u in table
orderby u.Status descending
select u).Take(1);

答案 2 :(得分:13)

您可以按状态分组并从最大的组中选择一行:

table.GroupBy(r => r.Status).OrderByDescending(g => g.Key).First().First();

第一个First()获取第一个组(状态最大的行集);第二个First()获得该组中的第一行 如果状态始终为unqiue,则可以将第二个First()替换为Single()

答案 3 :(得分:7)

解决第一个问题,如果您需要按照某些条件对多行进行分组,而另一列具有最大值,则可以执行以下操作:

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;

答案 4 :(得分:0)

更多示例:

关注:

 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);

等于:

 select t.*   from nametable t  where t.OrdSeq =
        (select max(t2.OrdSeq) from nametable t2 where t2.FieldPk= t.FieldPk)

答案 5 :(得分:-1)

简单一行:

var result = table.First(x => x.Status == table.Max(y => y.Status));

请注意,有两个动作。 内部动作是找到最大值, 外部动作是为了获得所需的对象。

相关问题