简体   繁体   English

根据分组删除列表中除1个对象外的所有对象

[英]Remove all but 1 object in list based on grouping

I have a list of objects with multiple properties in it. 我有一个包含多个属性的对象列表。 Here is the object. 这是对象。

public class DataPoint
{
    private readonly string uniqueId;
    public DataPoint(string uid)
    {
        this.uniqueId = uid;
    }

    public string UniqueId
    {
        get
        {
            return this.uniqueId;
        }
    }

    public string ScannerID { get; set; }

    public DateTime ScanDate { get; set; }
}

Now in my code, I have a giant list of these, hundreds maybe a few thousand. 现在在我的代码中,我有一个巨大的列表,数百甚至几千。

Each data point object belongs to some type of scanner, and has a scan date. 每个数据点对象属于某种类型的扫描仪,并具有扫描日期。 I want to remove any data points that were scanned on the same day except for the last one for a given machine. 我想删除在同一天扫描的任何数据点,除了给定机器的最后一个数据点。

I tried using LINQ as follows but this did not work. 我尝试使用LINQ如下,但这不起作用。 I still have many duplicate data points. 我还有很多重复的数据点。

this.allData = this.allData.GroupBy(g => g.ScannerID)
                   .Select(s => s.OrderByDescending(o => o.ScanDate))
                   .First()
                   .ToList();`

I need to group the data points by scanner ID, because there could be data points scanned on the same day but on a different machine. 我需要按扫描仪ID对数据点进行分组,因为可能会在同一天但在不同的计算机上扫描数据点。 I only need the last data point for a day if there are multiple. 如果有多个,我只需要一天的最后一个数据点。

Edit for clarification - By last data point I mean the last scanned data point for a given scan date for a given machine. 编辑以供说明 - 最后一个数据点是指给定机器的给定扫描日期的最后扫描数据点。 I hope that helps. 我希望有所帮助。 So when grouping by scanner ID, I then tried to order by scan date and then only keep the last scan date for days with multiple scans. 因此,当按扫描仪ID进行分组时,我尝试按扫描日期进行排序,然后仅将上次扫描日期保留为多次扫描的天数。

Here is some test data for 2 machines: 以下是2台机器的一些测试数据:

Unique ID   Scanner ID      Scan Date
A1JN221169H07  49374    2003-02-21 15:12:53.000
A1JN22116BK08  49374    2003-02-21 15:14:08.000
A1JN22116DN09  49374    2003-02-21 15:15:23.000
A1JN22116FP0A  49374    2003-02-21 15:16:37.000 
A1JOA050U900J  80354    2004-10-05 10:53:24.000 
A1JOA050UB30K  80354    2004-10-05 10:54:39.000 
A1JOA050UD60L  80354    2004-10-05 10:55:54.000 
A1JOA050UF80M  80354    2004-10-05 10:57:08.000 
A1JOA0600O202  80354    2004-10-06 08:38:26.000 

I want to remove any data points that were scanned on the same day except for the last one for a given machine . 我想删除在同一天扫描的任何数据点,除了给定机器的最后一个数据点。

So I assume you want to group by both ScanDate and ScannerID . 所以我假设您想要通过ScanDateScannerID Here is the code: 这是代码:

var result = dataPoints.GroupBy(i => new { i.ScanDate.Date, i.ScannerID })
                       .OrderByDescending(i => i.Key.Date)
                       .Select(i => i.First())
                       .ToList();

If I understand you correctly this is what you want. 如果我理解正确,这就是你想要的。

var result = dataPoints.GroupBy(i => new { i.ScanDate.Date, i.ScannerID })
                       .Select(i => i.OrderBy(x => x.ScanDate).Last())
                       .ToList();

This groups by the scanner id and the day ( SacnnerDate.Date will zero out the time portion), then for each grouping it orders by the ScanDate (since the groups are the same day this will order on the time) and takes the last. 这个按扫描仪ID和日期SacnnerDate.DateSacnnerDate.Date将零时间部分清零),然后对于每个分组,它由ScanDate命令(因为这些组是当时按顺序排列的那一天)并取最后一个。 So for each day you will get one result for each scanner which has the latest ScanDate for that particular day. 因此,对于每一天,您将获得每个扫描仪的一个结果,该扫描仪具有该特定日期的最新ScanDate

Just as an aside, the class could be defined as 除此之外,该课程可以定义为

public class DataPoint
{
  public DataPoint(string uid)
  {
    UniqueId = uid;
  }

public string UniqueId {get; private set; }
public string ScannerID { get; set; }
public DateTime ScanDate { get; set; }

} }

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

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