C# 如何在LINQ中仅选择日期最高的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/470440/
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-04 04:41:13  来源:igfitidea点击:

How to select only the records with the highest date in LINQ

c#.netlinqlinq-to-sql

提问by Bas Jansen

I have a table, 'lasttraces', with the following fields.

我有一个表“lasttraces”,其中包含以下字段。

Id, AccountId, Version, DownloadNo, Date

The data looks like this:

数据如下所示:

28092|15240000|1.0.7.1782|2009040004731|2009-01-20 13:10:22.000
28094|61615000|1.0.7.1782|2009040007696|2009-01-20 13:11:38.000
28095|95317000|1.0.7.1782|2009040007695|2009-01-20 13:10:18.000
28101|15240000|1.0.7.1782|2009040004740|2009-01-20 14:10:22.000
28103|61615000|1.0.7.1782|2009040007690|2009-01-20 14:11:38.000
28104|95317000|1.0.7.1782|2009040007710|2009-01-20 14:10:18.000

How can I, in LINQ to SQL, only get the last lasttrace of every AccountId (the one with the highest date)?

我怎样才能在LINQ to SQL中只获取每个 AccountId 的最后一个跟踪(日期最高的那个)?

采纳答案by Mehrdad Afshari

If you just want the last date for each account, you'd use this:

如果您只想要每个帐户的最后日期,则可以使用以下命令:

var q = from n in table
        group n by n.AccountId into g
        select new {AccountId = g.Key, Date = g.Max(t=>t.Date)};

If you want the whole record:

如果你想要整个记录:

var q = from n in table
        group n by n.AccountId into g
        select g.OrderByDescending(t=>t.Date).FirstOrDefault();

回答by bruno conde

It could be something like:

它可能是这样的:

var qry = from t in db.Lasttraces
          group t by t.AccountId into g
          orderby t.Date
          select new { g.AccountId, Date = g.Max(e => e.Date) };

回答by Sudhir Kesharwani

Go a simple way to do this :-

采取一种简单的方法来做到这一点:-

Created one class to hold following information

创建了一个类来保存以下信息

  • Level (number)
  • Url (Url of the site)
  • 级别(数量)
  • 网址(网站的网址)

Go the list of sites stored on a ArrayList object. And executed following query to sort it in descending order by Level.

转到存储在 ArrayList 对象上的站点列表。并执行以下查询以按级别降序对其进行排序。

var query = from MyClass object in objCollection 
    orderby object.Level descending 
    select object

Once I got the collection sorted in descending order, I wrote following code to get the Object that comes as top row

一旦我按降序对集合进行排序,我编写了以下代码来获取作为顶行的对象

MyClass topObject = query.FirstRow<MyClass>()

This worked like charm.

这就像魅力一样。

回答by Developer

Here is a simple way to do it

这是一个简单的方法来做到这一点

var lastPlayerControlCommand = this.ObjectContext.PlayerControlCommands
                                .Where(c => c.PlayerID == player.ID)
                                .OrderByDescending(t=>t.CreationTime)
                                .FirstOrDefault();

Also have a look this great LINQ place - LINQ to SQL Samples

也看看这个很棒的 LINQ 地方 - LINQ to SQL Samples

回答by Bob Zhang

If you want the whole record,here is a lambda way:

如果你想要整个记录,这是一种 lambda 方式:

var q = _context
             .lasttraces
             .GroupBy(s => s.AccountId)
             .Select(s => s.OrderByDescending(x => x.Date).FirstOrDefault());