从C#中的SortedSet中删除元素

时间:2019-04-11 12:27:18

标签: c# redis servicestack.redis

我正在尝试从Redis排序列表中删除一个元素,但没有成功

public bool Delete(int id)
        {
            try
            {
                var redisManager = new RedisManagerPool(Global.RedisConnector);
                using (var redis = redisManager.GetClient())
                {
                    var entities = redis.As<BookingRequestModel>();
                    var list = entities.SortedSets["BookingRequests"].GetAll();
     //count = 320
                    var model = list.First(z=>z.Id == id);
                    list.Remove(model);
           //count 319 - however when I refresh the page I still see my old data in grid ...
                    entities.Save();                  
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

也尝试过这样:


 public bool Delete(int id)
        {
            try
            {
                var redisManager = new RedisManagerPool(Global.RedisConnector);
                using (var redis = redisManager.GetClient())
                {
                    var entities = redis.As<BookingRequestModel>();
                    var list = entities.SortedSets["BookingRequests"];
                    var model = list.First(z=>z.Id == id);
                    var result = entities.RemoveItemFromSortedSet(list, model); // result is false                  
                    entities.Save();                  
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

正如我评论的那样,将旧数据从网格中删除后,我仍然可以看到它们。

您能帮我吗?

1 个答案:

答案 0 :(得分:0)

不知道为什么,但这是可行的:


public bool Delete(int id)
        {
            try
            {
                var redisManager = new RedisManagerPool(Global.RedisConnector);
                using (var redis = redisManager.GetClient())
                {                                   
                    var items = redis.GetAllItemsFromSortedSet("BookingRequests");
                    foreach (var item in items)
                    {
                        var dto = item.FromJson<BookingRequestModel>();
                        if (dto.Id == id)
                        {
                            redis.RemoveItemFromSortedSet("BookingRequests", item);
                            break;
                        }
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }