public static List <Message> GetCollection(MailDb maildb, SelectFolder Folder, SearchItem item) { if (item == null || item.Name == null) { return(maildb.Messages.FolderQuery(Folder.Folder).SelectAll()); } if (item.Name == "LARGER" || item.Name == "SMALLER") { int value = (int)item.Parameters["N"]; var msg = maildb.Messages.GetBySeqNo(Folder.Folder, Folder.Stat.LastestMsgId, Folder.Stat.Exists, value); if (msg == null) { throw new CommandException("NO", "not a valid sequence no. "); } if (item.Name == "LARGER") { return(maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.Id > msg.Id).SelectAll()); } else if (item.Name == "SMALLER") { return(maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.Id < msg.Id).SelectAll()); } } else if (item.Name == "UID") { var set = item.Parameters["SEQUENCE-SET"].ToString(); var range = Kooboo.Mail.Imap.ImapHelper.GetSequenceRange(set); ImapHelper.CorrectRange(range, Folder, true); List <Message> result = new List <Message>(); foreach (var uidrange in range) { for (int i = uidrange.LowBound; i <= uidrange.UpBound; i++) { var msg = maildb.Messages.Get(i); if (msg != null) { result.Add(msg); } } } return(result); } else { if (item.Name == "BEFORE" || item.Name == "SENTBEFORE") { var date = (DateTime)item.Parameters["DATE"]; var tick = date.Ticks; return(maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.CreationTimeTick < tick).SelectAll()); } else if (item.Name == "SENTSINCE" || item.Name == "SINCE") { var date = (DateTime)item.Parameters["DATE"]; var tick = date.Ticks; return(maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.CreationTimeTick > tick).SelectAll()); } else if (item.Name == "ON" || item.Name == "SENTON") { var date = (DateTime)item.Parameters["DATE"]; // get the day before and after, then filter... I think this has better performance because there is an index on tick. var before = date.AddDays(-1); var after = date.AddDays(1); var allmessages = maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.CreationTimeTick > before.Ticks && o.CreationTimeTick < after.Ticks).SelectAll(); if (allmessages != null && allmessages.Count() > 0) { return(allmessages.Where(o => o.CreationTime.DayOfYear == date.DayOfYear && o.CreationTime.Year == date.Year).ToList()); } } // string dateKey = "BEFORE SENTBEFORE SENTON SENTSINCE SINCE ON"; // only those left... } // not collection found, loop all. return(maildb.Messages.FolderQuery(Folder.Folder).SelectAll()); }