The type arguments for the method cannot be deprecated. Try to explicitly specify arguments like

I have a linq query that I am returning from Dictionary<int, string>

. I have an overloaded caching method that I created that will take an item Dictionary<T,T>

as one of the arguments. I have other methods in this class that take List<T>

and T[]

no problem. But this method refuses to compile with the theme theme error message.

This is my code for the caching class:

public static bool AddItemToCache<T>(string key, Dictionary<T, T> cacheItem, DateTime dt)
{
    if (!IsCached(key))
    {
        System.Web.HttpRuntime.Cache.Insert(key, cacheItem, null, dt, TimeSpan.Zero);

        return IsCached(key);
    }

    return true;
}

public static bool AddItemToCache<T>(string key, Dictionary<T, T> cacheItem, TimeSpan ts)
{
    if (!IsCached(key))
    {
        System.Web.HttpRuntime.Cache.Insert(key, cacheItem, null, Cache.NoAbsoluteExpiration, ts);

        return IsCached(key);
    }

    return true;
}

      

and this is a linq query that doesn't compile:

private Dictionary<int, string> GetCriteriaOptions()
{
    Dictionary<int, string> criteria = new Dictionary<int, string>();
    string cacheItem = "NF_OutcomeCriteria";

    if (DataCaching.IsCached(cacheItem))
    {
        criteria = (Dictionary<int, string>)DataCaching.GetItemFromCache(cacheItem);
    }
    else
    {
        Common config = new Common();
        int cacheDays = int.Parse(config.GetItemFromConfig("CacheDays"));

        using (var db = new NemoForceEntities())
        {
            criteria = (from c in db.OutcomeCriterias
                        where c.Visible
                        orderby c.SortOrder ascending
                        select new
                        {
                            c.OutcomeCriteriaID,
                            c.OutcomeCriteriaName
                        }).ToDictionary(c => c.OutcomeCriteriaID, c => c.OutcomeCriteriaName);

            if ((criteria != null) && (criteria.Any()))
            {
                bool isCached = DataCaching.AddItemToCache(cacheItem, criteria, DateTime.Now.AddDays(cacheDays));

                if (!isCached)
                {
                    ApplicationErrorHandler.LogException(new ApplicationException("Unable to cache outcome criteria"), 
                        "GetCriteriaOptions()", null, ErrorLevel.NonCritical);
                }
            }
        }
    }

    return criteria;
}

      

This is the isCached = DataCaching line ..... that I am getting the error. I tried translating it into dictionary ( Dictionary<int, string>

) by doing .ToDictionary()

, but nothing works.

Does anyone have any idea?

+3


source to share


2 answers


This won't compile because the key and value type of the dictionary must be the same, and yours are different. You can change the method definition to require the type of the string key:



public static bool AddItemToCache<T>(string key, Dictionary<string, T> cacheItem, TimeSpan ts)
{
    if (!IsCached(key))
    {
        System.Web.HttpRuntime.Cache.Insert(key, cacheItem, null, Cache.NoAbsoluteExpiration, ts);

        return IsCached(key);
    }

    return true;
}

      

+5


source


Change the parameter in your method signature from

Dictionary<T, T> cacheItem

      

to



Dictionary<TKey, TValue> cacheItem

      

T, T implies that key and value are of the same type.

+4


source







All Articles