C# 通过 EntityFramework 获取表的最新值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14981003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:39:25  来源:igfitidea点击:

Get the latest Value of table via EntityFramework

c#entity-framework

提问by Eric Nielsen

I have a table that have several field and each of them update separately by separate ViewModel , Now I wanna to get the latest Value of a specific field (maybe it has updated in fifth record of my table) , OK? now what I have write is look like this :

我有一个包含多个字段的表,每个字段都由单独的 ViewModel 分别更新,现在我想获取特定字段的最新值(也许它已在我的表的第五条记录中更新),好吗?现在我写的是这样的:

  public ViewResult ShowPiece()
        {
            var context = new SiteContext();
            var showPiece = context.Portraits.LastOrDefault();
            return View(showPiece);
        }

but when I run the application and navigate above action , I got thie Error :

但是当我运行应用程序并在操作上方导航时,我得到了错误:

LINQ to Entities does not recognize the method , and this method cannot be translated into a store expression...

LINQ to Entities 无法识别该方法,并且该方法无法转换为存储表达式...

what is the problem with that ??

这有什么问题??

回答by Sergey Berezovskiy

Use descending ordering (by date, or id) and FirstOrDefaultwhich is supported:

使用降序排列(按日期,或ID)和FirstOrDefault支持

var showPiece = context.Portraits
                       .OrderByDescending(p => p.Date)
                       .FirstOrDefault();


Another option, is select portrait which has max date (id) via subquery (as Evelie suggested in comments):

另一种选择是通过子查询选择具有最大日期(id)的肖像(如 Evelie 在评论中建议的那样):

var showPiece = context.Portraits
              .FirstOrDefault(p => p.Date == context.Portraits.Max(x => x.Date));

I made a little investigation here. In first case (ordering) following query is generated:

我在这里做了一点调查。在第一种情况下(排序)生成以下查询:

SELECT TOP (1) [t0].*
FROM [Portraits] AS [t0]
ORDER BY [t0].[Date] DESC

In second case (getting max):

在第二种情况下(获得最大值):

SELECT TOP (1) [t0].*
FROM [Portraits] AS [t0]
WHERE [t0].[Date] = ((
    SELECT MAX([t1].[Date])
    FROM [Portraits] AS [t1]
    ))

Execution plan is almost same, but in second case Top is executed twice. Thus Top costs 0% comparing to Index Scan, this should not be a problem.

执行计划几乎相同,但在第二种情况下 Top 执行了两次。因此与索引扫描相比,Top 成本为 0%,这应该不是问题。

回答by spajce

var s = con.Portraits.Where(j => j.Date.HasValue)
                  .OrderByDescending(a => a.Date)
                  .Select(p => p).FirstOrDefault();

回答by Tiago ávila

Try something like this:

尝试这样的事情:

var lastShowPieceId = context.Portraits.Max(x => x.Id);
return context.Portraits.FirstOrDefault(x => x.Id == lastShowPieceId);

I had that situation and this worked for me.

我遇到过这种情况,这对我有用。

回答by Victor Augusto Rodrigues

I've tried every of the replies that you've here.

我已经尝试了您在这里的所有回复。

But no one had really worked.

但没有人真正工作过。

My solution was:

我的解决方案是:

        List<Portraits> portraitsList = db.Portraits.ToList();
        int idPortraits = portraitsList.Last().PortraitsId;
        portratisList.Remove(portratisList.Last());

        Portraits p = db.Mementos.Find(idPortraits);