要从内存的List<QuestionsLibraryAndSort>对象中删除一条记录。语句是这样写的:
RedisKey.RedisQuestionsLibraryAndSort.Remove(message.QuestionsLibraryAndSort);
可是在前后输出list.count值,却发现没有改变。又再次做确认:
bool success = RedisKey.RedisQuestionsLibraryAndSort.Remove(message.QuestionsLibraryAndSort);
发觉success输出为 false 。这说明确实没删除。 message.QuestionsLibraryAndSort 是用消息队列RabbitMQ 传过来的 QuestionsLibraryAndSort 对象。在 RedisKey.RedisQuestionsLibraryAndSort 这个list 中不存在怀疑是编码的问题导致 Redis 中存在的 list 中的单个 QuestionsLibraryAndSort 与 RabbitMQ 传来的 QuestionsLibraryAndSort 不一致,导致RabbitMQ传来的QuestionsLibraryAndSort 与Redis 中的相应QuestionsLibraryAndSort 不完全一致而不能正确删除。我采取的一个解决办法是,利用 RemoveAll 方法来取代 Remove 方法。语句如下:
int success = RedisKey.RedisQuestionsLibraryAndSort.RemoveAll(m => m.GID == message.QuestionsLibraryAndSort.GID);
因为这个GID不为空,而且在行中是唯一的。用此搜索匹配记录并删除。经过测试 ,匹配行被顺利删除。 success=1 。问题解决