简体   繁体   English

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

[英]How to select only the records with the highest date in LINQ

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 的最后一个跟踪(日期最高的那个)?

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();

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

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());

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) };

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.转到存储在 ArrayList 对象上的站点列表。 And executed following query to sort it in descending order by Level.并执行以下查询以按级别降序对其进行排序。

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.这就像魅力一样。

LINQ's .OrderByDescending().FirstOrDefault() approach is good. LINQ 的.OrderByDescending().FirstOrDefault()方法很好。 But the https://github.com/morelinq/MoreLINQ people do it better: .MaxBy()但是https://github.com/morelinq/MoreLINQ的人做得更好: .MaxBy()

Example:例子:

IEnumerable<Person> data = Data.GetPersons();
Person oldestPerson = data.MaxBy(p => p.Age);

Or in the latest version of MoreLINQ, they have changed the return type:或者在最新版本的 MoreLINQ 中,他们改变了返回类型:

IEnumerable<Person> data = Data.GetPersons();
IEnumerable<Person> oldestPersons = data.MaxBy(p => p.Age);
Person oldestPerson = oldestPersons.First();

So what used to be .MaxBy() is now .MaxBy().First() .所以以前的.MaxBy()现在是.MaxBy().First() This has the advantage that you can handle more than 1 result (used to be only the first person with the max age, but there may be more persons with the same age).这样做的好处是您可以处理多个结果(过去只是最大年龄的第一个人,但可能有更多相同年龄的人)。 Another advantage is that .MaxBy() can now handle an empty IEnumerable<T> without throwing an exception.另一个优点是.MaxBy()现在可以处理空的IEnumerable<T>而不会引发异常。 Of course, the exception occurs in .First() then, but you can decide to use .FirstOrDefault() instead to avoid this.当然,异常发生在.First()中,但您可以决定使用.FirstOrDefault()来避免这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择一条记录,仅是LINQ to SQL中日期最高的子级 - How to select a record and only it's children with the highest date in LINQ to SQL 如何 select 记录具有特定日期的日期或今天的日期并增加 LINQ 中的最高序列号(int 或 id)? - How to select the records with the particular date day or today's date and increment highest sequence number (int or id) in LINQ? Linq选择最接近日期的记录 - Linq select records closest to date 选择日期最高的记录,并在其中包含相关表-LINQ - Select the record with highest date and INCLUDE relevant tables with it - LINQ 如何 select 记录用户的项目历史记录,其中 LINQ 到实体 lambda 中的项目的日期是最新的? - How to select records of user's history of items, where the date is latest for the items in LINQ to entities lambda? 如何使用LINQ选择具有最高价值的商品? - How do I select the item with the highest value using LINQ? 如何仅使用Linq从集合中选择与另一个表有关系的记录 - How to select only those records from collection which have relation with another table using Linq 如何使用 linq 从 C# 数据表 select 仅特定时间记录 - How to select only specific time records for each day from C# DataTable using linq C# Linq to entity - 如何仅选择具有数组中所有引用的记录 - C# Linq to entities - How to select only records which have all references from array LINQ - 如何 select 一个元素基于仅存在于某些记录中的另一个元素 - LINQ - How to select one element based on another element that exists in only some records
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM