使用Entity Framework 6返回具有最大列值的记录 [英] Return record with max column value using Entity Framework 6

查看:58
本文介绍了使用Entity Framework 6返回具有最大列值的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试获取 ReceivedDateTime 列的最大 datetime 值的记录,但是数据集应使用某些 Id进行预过滤列(不是唯一的).这样解决了:

Trying to get record with max datetime value for ReceivedDateTime column, however data set should be pre-filtered by some Id column (not unique). Solved this way:

using (var db = new SystemEntities())
{
    var records = db.Table.Where(p => p.Id == Id);
    var record = records.Where(p => p.ReceivedDateTime == records.Max(r => r.ReceivedDateTime)).FirstOrDefault();
    if(record != null)
    {

    }
}

有没有更漂亮,更简单,更短的实现方式?谢谢!

Is there more beautiful, simpler and shorter implementation, notation? Thanks!

推荐答案

您可以使用 OrderByDescending 简化如下操作:

You can simplify like the following using OrderByDescending:

using (var db = new SystemEntities())
{
    var record = db.Table.Where(p => p.Id == Id).OrderByDescending(x => x.ReceivedDateTime).FirstOrDefault();
    if(record != null){}
}

这篇关于使用Entity Framework 6返回具有最大列值的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆