我使用实体框架来连接MySQL。
我有一个实现subversion技术的增量数据。每个subversion记录集都有相同的LinkedId并用UpdatedTime分隔。
我的期望是从数据库中获取每个记录的最新版本。因此,我编写了一个linq语句,如下所示:

public List<Entry> LoadFinalEntries(int rptId) {
    return (from ent in ctx.Entries
           where ent.ReportId == rptId
           orderby ent.LinkedId, ent.UpdatedTime descending
           group ent by ent.LinkedId into svnEnt
           select svnEnt.FirstOrDefault()).ToList();
}

但是在运行时,它抛出一个EntityCommandCompilationException来告诉“不支持指定的方法”。我知道这个方法是FirstOrDefault,但无论如何都找不到修复它的方法。
请帮我找出另一种方法。

最佳答案

你觉得使用内部查询怎么样?

    return (from ent in ctx.Entries
            where ent.ReportId == rptId &&
            ent.UpdatedTime ==
                (from inner in ctx.Entries
                where inner.LinkedId == ent.LinkedId &&
                inner.ReportId == rptId
                select inner.UpdatedTime).Max()
            select ent).ToList();

08-18 06:56