所以我有这样一个表:
[Table("Box", Schema = "dbo")]
public partial class Box
{
[Column("eventId")]
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[StringLength(200)]
public int EventId {get;set;}
[Column("uuid")]
public int Uuid {get;set;}
[Column("localIndex")]
public int LocalIndex {get;set;}
[Column("rack")]
public string Rack {get;set;}
[Column("shelf")]
public string Shelf{get;set;}
[Column("fag")]
public string Fag{get;set;}
[Column("municipality")]
public string Municipality {get;set;}
[Column("organization")]
public string Organization {get;set;}
[Column("Entries")]
public string Entries{get;set;}
}
数据库是事件驱动的,因此特定盒子的 uuid 将保持不变,但其他一切都可能发生变化,所以我想要得到的是所有唯一 UUID 盒子的最新版本。我可以运行一些简洁的查询来完成这个吗?
作为一个临时解决方案,我现在只获取所有匹配特定 where 子句的框,并通过此函数运行结果:
let getLatest (list : Box list) =
list
|> List.filter(fun x -> list
|> List.forall(fun y ->
not (y.uuid = x.uuid && y.eventId > x.eventId)))
不用说,这可能不是最好的方法,但它确实有效。但是,如果有人有任何想法,我们将不胜感激。
最佳答案
我会通过按 UUID
对您的框进行分组,然后按 eventId
对每个组进行排序并从每个组中获取第一条记录来解决此问题。
在代码中它看起来像:
let getLatest2 (list : Box list) =
list
|> List.groupBy (fun x-> x.uuid)
|> List.map (fun (uuid, values) ->
values
|> List.sortByDescending (fun x->x.eventId)
|> List.head)
老实说,我不确定它是否会通过 EF 正确转换为 SQL 查询,或者它是否比您的原始解决方案有任何优势。
关于c# - Entity Framework 核心中的postgres查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57093153/