简体   繁体   English

Linq,为每个服务选择最新条目

[英]Linq, Select the latest entry for each service

I have the following table: 我有下表:

public class ServiceStatusHistory
{
    [Key]
    [Required]
    public int Id { get; set; }

    // Forenkey to service
    [Required]
    public virtual Service Service { get; set; }

    [Required]
    public ServiceStatus Status { get; set; }

    [Required]
    public string Messages { get; set; }

    [Required]
    //Time Logged in the db
    public DateTime time { get; set; }

    [Required]
    //Time the service last called the update method on the client
    public DateTime LastUpdateTime { get; set; }
}

Now I need to do a linq query that will. 现在,我需要执行一个linq查询。

Give me a list of ServiceStatusHistory Objects, for each service ID that there is in the db and only the latest(biggest time) one(for that service) where enabled = true. 给我一个ServiceStatusHistory对象的列表,用于数据库中存在的每个服务ID,并且只有最近(最长的时间)(针对该服务),其中enabled = true。

The reason that this is complicated is that, there will be multiple entries in the table for each service. 之所以复杂,是因为表中每个服务都有多个条目。 So a service with Id 4 could have 100 entries in here. 因此,编号为4的服务可以在此处包含100个条目。 and there will be multiple services. 并且会有多种服务。

here is my attempt: 这是我的尝试:

 using (var db = new EFDbContext())
            {
                var result = (from x in db.ServiceStatusHistory
                              where x.Service.Enabled == true
                              select x).FirstOrDefault();

                list =  result.toString();
            }

But I realised my query will only return one object not a list of them? 但是我意识到我的查询将只返回一个对象而不是它们的列表?

You could group by the service id and then select the last of the groupings. 您可以按服务ID分组,然后选择最后一个分组。

using (var db = new EFDbContext())
{
    var results = d.ServiceStatusHistory.Where(h => h.Service.Enabled)
                                        .OrderByDescending(h => h.time )
                                        .GroupBy(h => h.Service.Id)
                                        .Select(grp => grp.FirstOrDefault()); 
}

The OrderByDescending will give you the proper ordering for the FirstOrDefault . OrderByDescending将为您提供对FirstOrDefault的正确排序。 GroupBy preserves the ordering of the elements passed. GroupBy保留传递的元素的顺序。

Results will be an IEnumerable<ServiceStatusHistory> where you will have no repeated Service based on the Id property of the class. 结果将是IEnumerable<ServiceStatusHistory> ,其中您将不会基于类的Id属性重复使用Service Change the OrderByDescending if your "last" has a specific meaning. 如果您的“最后一个”具有特定含义,请更改OrderByDescending

Edit: To eagerly include an object, you can use the Include extension method: 编辑:要热切包含一个对象,可以使用Include扩展方法:

d.ServiceStatusHistory.Include(h => h.Service)

You can try change to this: 您可以尝试更改为:

  (from x in db.ServiceStatusHistory
                                  where x.Service.Enabled == true
                                  select x).Last();

Try this 尝试这个

(from x in db.ServiceStatusHistory
                                  where x.Service.Enabled == true
                                  select x).OrderBydescending(x => x.yourfeild).Top(1);
db.ServiceStatusHistory
    .Where(x => x.Status.Enabled)
    .GroupBy(x => x.Service)
    .Select(x => x.OrderByDescending(y => y.time).First());

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM