我有一个SQL表,正在使用Entity Framework Core。在将记录从Excel文件上传到此表的过程中,我还添加了一个简单的datetime
的“上传日期”(DateTime.Today
)列。
例如:
record | id | employee | customer| project | hours | uploadDate |
现在,我想要一个仅显示记录最新上传的视图。你知道一个好的lambda / LINQ吗?
_context.TableWithRecords.Where(DateTime.Max?).ToList();
基本上或类似的东西。
答案 0 :(得分:2)
听起来您想要这样的东西:
@Override
public void start(Stage primaryStage) {
VBox content = new VBox();
ScrollPane scrollPane = new ScrollPane(content);
VBox.setVgrow(scrollPane, Priority.ALWAYS);
Button button = new Button("fill");
button.setOnAction(evt -> {
for (int i = 0; i < 20; i++) {
content.getChildren().add(new Text(Integer.toString(i)));
}
System.out.println("content size before layout: " + content.getHeight());
// manually layout scrollPane
scrollPane.applyCss();
scrollPane.layout();
System.out.println("content size after layout: " + content.getHeight());
scrollPane.setVvalue(1d);
});
Scene scene = new Scene(new VBox(button, scrollPane), 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
这将按上传日期进行分组,然后按降序排列以使最近上传的组成为第一组,然后抓取第一组并返回所有这些结果。
使用以下示例数据进行测试:
_context
.TableWithRecords
.GroupBy(x => x.uploadDate)
.OrderByDescending(x => x.Key)
.First()
.ToList();
其中var dates = new List<TestRecord>
{
new TestRecord { Record = "1", Date = new DateTime(2018, 8, 1) },
new TestRecord { Record = "2", Date = new DateTime(2018, 8, 1) },
new TestRecord { Record = "3", Date = new DateTime(2018, 8, 1) },
new TestRecord { Record = "4", Date = new DateTime(2018, 8, 2) },
new TestRecord { Record = "5", Date = new DateTime(2018, 8, 2) },
new TestRecord { Record = "6", Date = new DateTime(2018, 8, 2) },
};
定义为:
TestRecord
给出期望的输出:
public class TestRecord
{
public string Record { get; set; }
public DateTime Date { get; set; }
}
答案 1 :(得分:0)
我将使用两个查询:
DateTime? maxDate = _context.TableWithRecords.Max(x => x.uploadedDate);
var result = _context.TableWithRecords.Where(x => x.uploadDate == maxDate).ToList()