C# 在HttpContext.Current.Items缓存中使用lambda表达式查找键

C# 在HttpContext.Current.Items缓存中使用lambda表达式查找键,c#,lambda,httprequest,C#,Lambda,Httprequest,我有一个请求缓存,使用HttpContext.Current.Items实现,如下所示: private static readonly Lazy<CacheCurrentCall> lazy = new Lazy<CacheCurrentCall>(() => new CacheCurrentCall()); public static CacheCurrentCall Instance { get { IDictiona

我有一个请求缓存,使用HttpContext.Current.Items实现,如下所示:

private static readonly Lazy<CacheCurrentCall> lazy =
    new Lazy<CacheCurrentCall>(() => new CacheCurrentCall());

public static CacheCurrentCall Instance
{
    get
    {
        IDictionary items = HttpContext.Current.Items;
        if (!items.Contains("CacheCurrentCall"))
        {
            items["CacheCurrentCall"] = new CacheCurrentCall();
        }
        return items["CacheCurrentCall"] as CacheCurrentCall;
    }
}

private CacheCurrentCall()
{

}

public void Add<T>(T o, string key, int cacheDurationSeconds = 0)
{
    HttpContext.Current.Items.Add(key, o);
}

public void Clear(string key)
{
    HttpContext.Current.Items.Remove(key);
}

public bool Exists(string key)
{
    return HttpContext.Current.Items[key] != null;
}

public bool Get<T>(string key, out T value)
{
    try
    {
        if (!Exists(key))
        {
            value = default(T);
            return false;
        }
        value = (T)HttpContext.Current.Items[key];
    }
    catch
    {
        value = default(T);
        return false;
    }
    return true;
}
该属性是一个
IDictionary
,因此您必须执行以下操作:

public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return HttpContext.Current.Items
        .Cast<DictionaryEntry>()
        .Where(e => e.Key is string && 
                    e.Value is string && 
                    condition(e.Key as string))
        .Select(e => e.Value as string);
}
public IEnumerable GetKey(Func条件)
{
返回HttpContext.Current.Items
.Cast()
其中(e=>e.Key是字符串&&
e、 值为字符串&&
条件(如键作为字符串))
.选择(e=>e.值作为字符串);
}
或在查询语法中:

public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return
        from e in HttpContext.Current.Items.Cast<DictionaryEntry>()
        where e.Key is string && 
              e.Value is string && 
              condition(e.Key as string)
        select e.Value as string;
}
public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return
        from k in HttpContext.Current.Items.Keys.OfType<string>()
        where condition(k)
        select k;
}
public IEnumerable GetKey(Func条件)
{
返回
来自HttpContext.Current.Items.Cast()中的e
其中e.Key是字符串&&
e、 值为字符串&&
条件(例如,键作为字符串)
选择e值作为字符串;
}

更新我错过了阅读问题的机会。我以为您希望根据键的特定标准选择值。如果只想选择关键点,实际上会更容易:

public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return HttpContext.Current.Items.Keys
        .OfType<string>()
        .Where(condition);
}
public IEnumerable GetKey(Func条件)
{
返回HttpContext.Current.Items.Keys
第()类
.在何处(条件);
}
或在查询语法中:

public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return
        from e in HttpContext.Current.Items.Cast<DictionaryEntry>()
        where e.Key is string && 
              e.Value is string && 
              condition(e.Key as string)
        select e.Value as string;
}
public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return
        from k in HttpContext.Current.Items.Keys.OfType<string>()
        where condition(k)
        select k;
}
public IEnumerable GetKey(Func条件)
{
返回
从HttpContext.Current.Items.Keys.OfType()中的k开始
何处条件(k)
选择k;
}

您试过什么吗?您自己在实施此解决方案时遇到了哪些问题?您是否收到错误、输出不正确或是什么?谢谢,我最终需要对您的代码做一个小改动(问题不太清楚,是我的错)(请参阅第二次编辑),但它可以工作。@很抱歉,我一开始误解了您的问题。有关更好的解决方案,请参阅我的更新答案。
public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return HttpContext.Current.Items.Keys
        .OfType<string>()
        .Where(condition);
}
public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return
        from k in HttpContext.Current.Items.Keys.OfType<string>()
        where condition(k)
        select k;
}