C# 按索引获取应用程序状态键名称
我正在向应用程序状态添加几个值,如:C# 按索引获取应用程序状态键名称,c#,asp.net,C#,Asp.net,我正在向应用程序状态添加几个值,如: for(int i=0;i<MyList.Count;i++) Application[MyList[i].Id.ToString()] = MyList[i].Value; 对于(int i=0;i您可以使用以下集合: foreach (string key in Application.AllKeys) { int id; if (Int32.TryParse(key, out id)) { if
for(int i=0;i<MyList.Count;i++)
Application[MyList[i].Id.ToString()] = MyList[i].Value;
对于(int i=0;i您可以使用以下集合:
foreach (string key in Application.AllKeys)
{
int id;
if (Int32.TryParse(key, out id))
{
if (!MyList.Any(l => l.Id == id))
{
Application.Remove(key);
}
}
}
回答得很好。非常感谢。为什么我没有想到所有的钥匙呢?太尴尬了
for(int i=0;i<MyList.Count;i++)
Application[MyList[i].Id.ToString()] = MyList[i].Id.ToString() + "," + MyList[i].Value;
for(int i=0;i<Application.Count;i++)
{
int Id = int.Parse(Application[i].Split(',')[0]);
if(!MyList.Any(l => l.Id == Id) Application[Id.ToString()] = null;
}
foreach (string key in Application.AllKeys)
{
int id;
if (Int32.TryParse(key, out id))
{
if (!MyList.Any(l => l.Id == id))
{
Application.Remove(key);
}
}
}