简体   繁体   English

LINQ 查询按最旧和按 id 选择

[英]LINQ query to select by oldest and by id

I am having this issue to select items.我在选择项目时遇到了这个问题。 By now I am using this query:现在我正在使用这个查询:

var eventQuery = dbContext.ClientEvents
                          .Where(f => f.ClientEventStatusLogs.Any(e => e.StatusId == 2))
                          .Select(TransformEvent());

ClientEvents is a list of events and every event can have another list of ClientEventStatusLogs . ClientEvents是一个事件列表,每个事件都可以有另一个ClientEventStatusLogs列表。 This query works fine most of the time.大多数情况下,此查询都可以正常工作。 But If I have new ClientEventStatusLogs on this event with StatusId not 2 it will still gives me this ClientEvent , because of ANY.但是如果我在这个事件上有新的ClientEventStatusLogs并且StatusId不是 2 它仍然会给我这个ClientEvent ,因为任何。 ClientEventStatusLogs have date column, so I would like to check if StatusId == 2 only for the latest log. ClientEventStatusLogs有日期列,所以我想检查if StatusId == 2仅用于最新日志。

I hope you understand my problem.我希望你明白我的问题。

It sounds like you want听起来你想要

dbContext.ClientEvents.Where(
    f => f.ClientEventStatusLogs
        .OrderByDescending(e => e.Date)
        .FirstOrDefault()?.StatusId == 2)

Just change Date to whatever the date property name is.只需将Date更改为日期属性名称即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM