Search code examples
c#listconsole-applicationdataservice

Finding latest record in a List


I have a list rms.PositiveResponse, and another list rms.NegativeResponses. The Lists contain a RecruitID and a Timestamp. I have over 10,000 records.

I am getting this data from a web service.

The problem is finding if the latest update of a RecruitID was a positive response or negative response. How can I determine that? I have the Timestamp for each RecruitID, the latest timestamp can tell me the latest update. How can I know what the latest update of a RecruitID was so that I can store it in the database?

Here was my attempt, but this is very slow method of comparing , I want to know if there is a faster way.

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

Solution

  • Try this:

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