user4131888
user4131888

Reputation:

The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly

I've got a linq query that I am returning a Dictionary<int, string> from. I've got an overloaded caching method, that I have created that will take a Dictionary<T,T> item as one of the arguments. I've got some other methods in this class that take List<T> and T[] without issue. But this one method, refuses to compile with the error message of the thread subject.

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 the linq query that is failing to 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;
}

It's the line isCached = DataCaching..... that I am getting the error. I've tried casting it to a dictionary (Dictionary<int, string>), doing a .ToDictionary(), but nothing works.

Anyone got any ideas?

Upvotes: 2

Views: 16847

Answers (2)

Allan Elder
Allan Elder

Reputation: 4104

Change the parameter in your method signature from

Dictionary<T, T> cacheItem

to

Dictionary<TKey, TValue> cacheItem

T, T implies that the key and the value have the same type.

Upvotes: 4

Lee
Lee

Reputation: 144206

This fails to compile because the key and value type of the dictionary needs to be the same, while yours are different. You could change the definition of the method to require a string key type:

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

Upvotes: 6

Related Questions