gpt4 book ai didi

c# - 在 C# 中从 SortedSet 中删除元素

转载 作者:IT王子 更新时间:2023-10-29 06:16:26 25 4
gpt4 key购买 nike

我试图从 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;
}
}

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

你能帮我解决这个问题吗?

最佳答案

不明白为什么,但这是有效的:


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

关于c# - 在 C# 中从 SortedSet 中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55632519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com