所以我有一个这样的表:
[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)))不用说,这可能不是最好的方法,但它是有效的。然而,如果任何人有任何想法,将不胜感激。
发布于 2019-07-18 20:23:46
我会解决这个问题,将你的盒子按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查询,或者它是否比你的原始解决方案有任何优势。
https://stackoverflow.com/questions/57093153
复制相似问题