查找列表中的最新记录

时间:2012-07-19 08:06:14

标签: c# list console-application dataservice

我有一个列表rms.PositiveResponse和另一个列表rms.NegativeResponse。列表包含RecruitIDTimestamp。我有超过10,000条记录。

我从网络服务获取此数据。

问题是找出RecruitID的最新更新是肯定响应还是否定响应。我该如何确定?我为每个Timestamp设置了RecruitID,最新的时间戳可以告诉我最新的更新。我怎么知道RecruitID的最新更新是什么,以便我可以将它存储在数据库中?

这是我的尝试,但这是比较慢的方法,我想知道是否有更快的方法。

RMSDataService.RMS rms = new RMSDataService.RMS();
var negList = rms.NegativeResponse.Where(d => d.RLMSTimeStamp != null && d.RLMSTimeStamp > new DateTime(2012, 02, 22));
var posList = rms.PositiveResponse.Where(d => d.RLMSTimeStamp != null && d.RLMSTimeStamp > new DateTime(2012, 02, 22));

foreach (var pos in posList)
{
  foreach(var neg in negList)
  {
    if(neg.RLMSRecruitId == pos.RLMSRecruitId && neg.RLMSTimestamp > pos.RLMSTimestamp)
    {
      Console.WriteLine("Item fetched: RecruitId:{0} NegTimeStamp:{1} PosTimeStamp:{2}", neg.RLMSRecruitId, neg.RLMSTimeStamp, pos.RLMSTimeStamp);
    }
   }
}

2 个答案:

答案 0 :(得分:1)

试试这个:

var negList = rms.NegativeResponse.Where(d => d.RLMSTimeStamp != null && d.RLMSTimeStamp > new DateTime(2012, 02, 22)).toList();
var posList = rms.PositiveResponse.Where(d => d.RLMSTimeStamp != null && d.RLMSTimeStamp > new DateTime(2012, 02, 22)).toList();


var item = (from pos in posList
            join neg in negList
              on
                 pos.RLMSRecruitId equals neg.RLMSRecruitId
              orderby pos.RLMSTimestamp descending
              select pos).FirstOrDefault();

答案 1 :(得分:1)

查看问题的一种方法是,您遇到的主要问题是源数据没有指示响应是+ ve还是-ve。因此,创建一个具有该指标的新列表,然后您可以轻松找到每个Id 响应类型的最新时间记录。

e.g。

class Record {
    public int  Id { get; set; }
    public DateTime Timestamp { get; set; }
}

private Record[] PositiveResponses = new[]{
    new Record{ Id =1 , Timestamp = new DateTime(2012,1,1)},
    new Record{ Id =1 , Timestamp = new DateTime(2012,1,2)},
    new Record{ Id =2 , Timestamp = new DateTime(2012,1,3)},
    new Record{ Id =2 , Timestamp = new DateTime(2012,1,5)}, // latest for id 2
    new Record{ Id =3 , Timestamp = new DateTime(2012,1,8)},
    new Record{ Id =4 , Timestamp = new DateTime(2012,1,8)}  // latest for Id 4
};

private Record[] NegativeResponses = new[]{
    new Record{ Id =1 , Timestamp = new DateTime(2012,1,1)},
    new Record{ Id =1 , Timestamp = new DateTime(2012,1,3)}, // latest for Id 1
    new Record{ Id =2 , Timestamp = new DateTime(2012,1,4)},
    new Record{ Id =3 , Timestamp = new DateTime(2012,1,9)}  // latest for id 3
};


[TestMethod]
public void GetLatest() {

    var results = PositiveResponses.Select(r => new {
                                                     Id = r.Id, 
                                                     Timestamp = r.Timestamp, 
                                                     Type = "Pos"
                                                 })
                                    .Union(
                  NegativeResponses.Select(r => new {Id = r.Id, 
                                                     Timestamp = r.Timestamp,
                                                     Type = "Neg"}))
                  .GroupBy(r => r.Id)
                  .Select(grp => grp.OrderByDescending(r => r.Timestamp).First());

    }

我们从每个列表中选择一个新的(匿名)记录,其中包含Id,时间戳和类型“Pos”或“Neg”,然后将它们合并为一个新列表。

获得新列表后,可以轻松按ID分组,然后选择时间戳最高的记录。此记录将具有Id,Timestamp和Pos / Neg指示符。