0

I was not sure how to write the title, but anyway. I have a method that randomly sends me objects (of emails) from 10 to 60 seconds. Now is the problem that I need to make a new method called getNewMessages, that returns a list of emails that is NEW from the last time the method is called.

So lets say that the first time I call this method it gets all the new emails (this part is not a problem), but the second time I call it, the old emails should be removed from the list and the new ones should fill the list, SINCE the last time we called the method.

MrProgram
  • 5,044
  • 13
  • 58
  • 98

2 Answers2

0

Make an enclosing entity so will have the last date retrieved then use a Linq query to get only the new ones

Sample:

var result = list.Where(v=>v.LastUpdate > _lastUpdate);
_lastUpdate = DateTime.Now;
result.ToList().ForEach(v=>v.LastUpdate = _lastUpdate);
Yacov
  • 1,060
  • 14
  • 27
0

Your question is quite vague, so it is quite hard to give a concrete answer. But maybe some ideas may help you.

Every mail you receive gets some kind of unique id. This id could in your case derived from the sender and the timestamp of the received mail. Maybe you should write an IEqualityComparer and consider this GetHashCode algorithm for your implementation. Then you simply have to put all your mails into a new HashSet<Email>(MyEqualityComparer) and when your method gets called you can simply check there if you already computed this message or not.

If you don't like to store all mails within the HashSet<T> (maybe to improve memory footprint) you could call the MyMailComparer.GetHashCode(email) and put all the results into a HashSet<int> and discard the mail afterwards. But be aware that this could possible drop a new message cause it would be possible that two different messages could produce the same hash code, hence the need for the Equals() method to check in this case if both message are really equal, but this would make it necessary to keep the mails within the memory.

Community
  • 1
  • 1
Oliver
  • 43,366
  • 8
  • 94
  • 151