I just recently upgraded this project from ASP.Net 3.5 to 4.0 so that I could use the concurrentDictionary instead of Dictionary because of the thread safe feature.
To use it I created an extension using code found in help forums.
It is all very close to working and I don't know how I can modify the extension for it to work properly.
Here is the code:
var catalogs = (from _catalog in entities.catalogs
from rolePermission in entities.c_roleperm
from _group in entities.c_group
from _user in entities.c_user
where _group.id == rolePermission.groupID
&& rolePermission.roleID == user.roleID
&& _catalog.groupID == rolePermission.groupID
&& _user.id == _catalog.userID
select new { name = _catalog.name, groupID = _catalog.groupID, userName = _user.name, userID = _catalog.userID, groupName = _group.name, ID = _catalog.id }
);
var listItems = catalogs.ToList(p => new CatalogItem() { name = p.name, groupID = p.groupID, userID = p.userID, username = p.userName, groupName = p.groupName, ID = p.ID }).GroupBy(p => p.groupName).ToConcurrentDictionary(p => p.Key, p => p.ToList());
And the code in the extension:
public static class Extentions
{
public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> source)
{
return new ConcurrentDictionary<TKey, TValue>(source);
}
public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue>(
this IEnumerable<TValue> source, Func<TValue, TKey> keySelector)
{
return new ConcurrentDictionary<TKey, TValue>(
from v in source
select new KeyValuePair<TKey, TValue>(keySelector(v), v));
}
And this is the error that I receive:
Error 1 No overload for method 'ToConcurrentDictionary' takes 2 arguments
What would I need to modify for the extension to work in this situation? Any suggestions are greatly appreciated.
The Func keyword is actually more like a function that returns a value. You might be looking at an "Expression" to pass in that kind of stuff. Something close to this:
No code guarentee, but I would suggests reading THIS POST, and the Expression Class from MSDN
A variant that does not require an intermediate conversion to a
KeyValue
pair, and matches Linq's implementation ofToDictionary
.You don't have an overload which allows you to extract the value from an item: